为什么不能引用通过表达式类型? [英] Why can not reference a type through an expression?
问题描述
下面的代码似乎是不可能编译不管我如何努力尝试投它?P能有人为请告诉我,我做错了。
公共类LUOverVoltage
{
公共字符串名称{;组; }
公共枚举OVType {OVLH,OVLL}
公开名单<串GT; PinGroups =新的List<串GT;();
公共无效添加(字符串名称,OVType类型,字符串GRP)
{
this.Name =名称;
this.OVType =类型; //为什么不能通过表达式引用类型?
PinGroups.Add(GRP);
}
}
您'重新混淆具有与枚举类型本身枚举类型的字段。你的代码是约的话说字符串=喇嘛
。
公共枚举OVType {OVLH,OVLL}
公共类LUOverVoltage
{
公共字符串名称{;组; }
公共OVType OVType {搞定;组; }
这声明了一个名为键入 OVType
和具有相同名称的属性。现在,你的代码应该工作。
作为一个方面说明,无论你的类型名称和属性名违反.NET命名准则。
我命名枚举类型 OverVoltKind
和酒店仅类
。
The following code seems to be impossible to compile no matter how hard I am trying to cast it :P can somone please tell me what I am doing wrong?
public class LUOverVoltage
{
public string Name { get; set; }
public enum OVType { OVLH, OVLL }
public List<string> PinGroups = new List<string>();
public void Add(string name, OVType type, string Grp)
{
this.Name = name;
this.OVType = type; //Why cannot reference a type through an expression?
PinGroups.Add(Grp);
}
}
You're confusing a field that has an enum type with the enum type itself. Your code is about as useful as saying string="bla"
.
public enum OVType { OVLH, OVLL }
public class LUOverVoltage
{
public string Name { get; set; }
public OVType OVType { get; set; }
This declares a type called OVType
and a property with the same name. Now your code should work.
As a side note, both your type names, and the property names violate .net naming guidelines.
I'd name the enum type OverVoltKind
and the property just Kind
.
这篇关于为什么不能引用通过表达式类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!