从属性的表示中获取属性
我对这个标题有一些疑问,但我想不出更好的。
假设我有以下枚举
public enum ClassProperties
{
Primary = 0,
Secondary = 1,
}
和一个看起来像这样的类
public class Test
{
Primary { get { return _primary; }}
Secondary { get { return _secondary; }}
// more irrelevant properties
}
现在我需要迭代枚举并使用其中的每个项目来获取属性,如下所示:
foreach(ClassProperties myProp = Enum.GetValues(typeof(ClassProperties)))
{
Test t = new Test();
t.myProp // <- this is what I'm after
// so if myProp equals Primary,
// t.Primary is called...
}
这会让您了解我正在尝试的内容想做,但尝试让我感觉很脏,就像一个刚刚尿湿了自己的流浪汉一样。就是感觉不太对劲。
I have some doubt about the title, but I couldn't come up with anything better.
Say I have the following enum
public enum ClassProperties
{
Primary = 0,
Secondary = 1,
}
And a class that looks this
public class Test
{
Primary { get { return _primary; }}
Secondary { get { return _secondary; }}
// more irrelevant properties
}
Now somewhere along the line I need to iterate over the enumeration and use each item in it to get the property, like so:
foreach(ClassProperties myProp = Enum.GetValues(typeof(ClassProperties)))
{
Test t = new Test();
t.myProp // <- this is what I'm after
// so if myProp equals Primary,
// t.Primary is called...
}
This would give you an idea of what I'm trying to do, but trying it makes me feel dirty like a bum who just wet himself. It just doesn't feel right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那么您可以使用反射来检索属性。然后,这将根据名称找到该属性。
有关更多信息,请参阅 GetProperties() 方法 &返回的 PropertyInfo 类型。
Well you could use Reflection to retrieve the properties. This will then locate the property based on its name.
For more see GetProperties() method & the returned PropertyInfo type.
枚举和关联属性是动态的吗?如果是的话,您将需要使用反射。否则,您可以执行一个简单的 if-then 语句。
Is the enumeration and associated properties dynamic? If they are you will want to use reflection. Otherwise, you could do a simple if-then statement.
至少有两种方法可以做到这一点:
1.Reflection 和 PropertyInfo
不过,您应该注意性能,只是对具有两个属性设置的类进行粗略测试并获取所有属性 10k 次反射需要 0.06 秒,如果我手写则需要 0.001 秒。因此,性能损失非常严重
2.动态方法
这种方法比较复杂,但是性能非常值得。动态方法是程序在运行时为其发出 MSIL 的方法。它们由运行时执行,就像它们是由编译器创建一样(因此速度非常好)。使用此方法设置并获取类上的 2 个属性 10k 次需要 0.004 秒(相比之下,反射为 0.06 秒,手动为 0.001 秒)。下面是为某种类型的 getter 和 setter 生成委托数组的代码。生成动态的成本可能很高,因此如果您打算多次使用委托(您可能会这样做),则应该缓存委托。
动态方法的调用:
Obs:上面的代码不处理没有 getter 或 setter 或标记为 private 的属性。通过查看 ProperyInfo 类的属性并仅在适当的情况下创建委托,可以轻松完成此操作
There are at least two ways of doing this:
1.Reflection and PropertyInfo
You should be careful with regard to performance though, just a rough test for a class with two properties setting and getting all the properties 10k times takes 0.06 seconds with reflection and 0.001 seconds if I write it by hand. So the performance hit is pretty drastic
2.Dynamic Methods
This method is more complicated but the performance is well worth it. Dynamic methods are methods which the program emits MSIL for at runtime. They get executed by the runtime as if they were created by the compiler (so the speed is very good). Using this method setting and getting 2 properties on a class 10k times took 0.004 seconds (compared to 0.06 seconds with reflection and 0.001 second by hand). Bellow is the code to generate an array of delegates for getters and setters for a certain type. Generating the dynamic can be costly so you should cache the delegates if you intend to use the multiple times (which you probably will).
Invocation of the dynamic methods:
Obs: The code above does not deal with properties that have no getter or no setter or are marked as private. This could be easily done by looking at the properties of the ProperyInfo Class and only creating the delegates if apropriate