C# 根据枚举值获取公共变量的类型
我有一个类可以解析逗号分隔文本文件中的数据。我有一个字段枚举,可以帮助我更轻松地解析数据。解析中所有记录的类保存每个字段的公共变量,当然还有它们的变量类型。我需要根据给定的枚举获取这些变量的类型。
public enum DatabaseField : int
{
NumID1 = 1,
NumID2 = 2,
NumID3 = 3,
};
public class DataBaseRecordInfo
{
public long NumID1 { get; set; }
public int NumID2 { get; set; }
public short NumID3 { get; set; }
public static Type GetType(DatabaseField field)
{
Type type;
switch (field)
{
case DatabaseField.NumID1:
type = typeof(long);
break;
case DatabaseField.NumID2:
type = typeof(int);
break;
case DatabaseField.NumID3:
type = typeof(short);
break;
default:
type = typeof(int);
break;
}
return type;
}
};
NumID1、NumID2、NumID3 都在我的构造函数中分配。但是,我希望在不创建 DataBaseRecordInfo 实例的情况下获取这些类型。现在我上面的静态方法可以工作,但是,如果我想更改变量类型,我必须在两个地方更改它。有没有办法避免在两个地方都更改它并将其保留为静态方法?
I have a class that parses in data from a comma delimited text file. I have an enum for the fields to help me parse data in easier. The class that parses all the records in holds public variables for each field, and of course their variable types. I need to get the type of these variables based on the enum given.
public enum DatabaseField : int
{
NumID1 = 1,
NumID2 = 2,
NumID3 = 3,
};
public class DataBaseRecordInfo
{
public long NumID1 { get; set; }
public int NumID2 { get; set; }
public short NumID3 { get; set; }
public static Type GetType(DatabaseField field)
{
Type type;
switch (field)
{
case DatabaseField.NumID1:
type = typeof(long);
break;
case DatabaseField.NumID2:
type = typeof(int);
break;
case DatabaseField.NumID3:
type = typeof(short);
break;
default:
type = typeof(int);
break;
}
return type;
}
};
NumID1, NumID2, NumID3 all get assigned within my constructor. However, I want to get these types without ever creating an instance of DataBaseRecordInfo
. Right now my static method above would work, however, if I wanted to change the variable type, I would have to change it in 2 places. Is there a way to get around having to change this in both places and keep it as a static method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果名称始终完全匹配,您可以使用反射来完成此操作。
您甚至可以将这些值缓存在字典中,因此如果找到,只需返回字典条目,否则使用反射确定并缓存结果。
If the name is always going to match exactly you can do this using reflection.
You could even cache these values in a dictionary, so if found, just return the dictionary entry, otherwise determine using reflection and cache the result.
是的,您可以使用枚举中的名称以及 DatabaseRecordInfo 类型的反射来获取您需要的类型。
这可以这样完成:
您可能希望将结果缓存在字典中,因为反射在性能方面可能会很昂贵。
Yes, you can use the names in the enum together with reflection on the DatabaseRecordInfo type to get the types you need.
This could be done like this:
You will probably want to cache the result in a dictionary, since the reflection can be expensive performance-wise.
像这样的事情怎么样:
我知道你说过不必创建
DataBaseRecordInfo
的实例,但我假设你指的是静态方法之外的实例。没有人见过这个例子。How about something like this:
I know you said without ever having to create an instance of
DataBaseRecordInfo
but I'm assuming you meant an instance outside of the static method. No one ever sees this instance.如果您想将枚举值与一些附加信息绑定,您可以使用自己的 CustomAttribute。
也许你需要这样的东西:
If you want to bind enum value with some additional information you can use your own CustomAttribute.
Maybe you need something like this: