使用反射(或其他方式)获取成员对象
我希望能够通过名称访问属性或类的成员。如果它是一个属性,我很好:
PropertyInfo prop = object.GetType().GetProperty(propertyName);
object propValue = prop.GetValue(object, null);
如果它是一个成员/字段,事情似乎有点棘手。在研究了 GetMember 并尝试了一堆不同的东西之后,我不知道如何以与从 GetProperty 返回对象相同的方式获取对象的引用,如果这是一种财产。
另一个问题是,这是实现这一目标的正确方法吗?在阅读有关反射的内容时,似乎有很多开销。这是否只是与不使用反射相比,或者它是否足够重要,以至于我应该三思而行开发一些将经常这样做的东西,以及我有什么替代方案(如果有的话)来获取对 a 成员的引用仅按名称分类?
感谢您的任何见解。如果有什么区别的话,我的目标是能够序列化任意命名的属性或类的成员。
I want to be able to access a property or member of a class by name. I'm fine if it's a property:
PropertyInfo prop = object.GetType().GetProperty(propertyName);
object propValue = prop.GetValue(object, null);
Things seem a little trickier if it is a member/field. AFter studying GetMember
and trying a bunch of different stuff I can't figure out how to just get a reference to the object the same way it would be returned from GetProperty
, if it was a property.
The other question is, is this the right way to accomplish this? In reading about reflection, it seems to have a lot of overhead. Is this just in comparison to not using reflection, or is it significant enough that I should think twice about developing something that will be doing this a lot, and what alternatives do I have, if any, to obtain a reference to a member of a class by name only?
Thanks for any insight. If it makes any difference, my goal is to be able to serialize an arbitrary named property or member of a class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 GetField 而不是
GetProperty
如果你想要检索一个字段。它肯定很昂贵,但我会对其进行分析以确定您是否会遇到性能问题。
另一个主要选项是自己构建某种形式的字典,将“名称”映射到值。这可以是映射到值的直接字典,也可以是映射到检索值的委托的字典。这样做的优点是,您可以使其以相同的方式用于属性或字段,但每个类都需要创建映射。 (然而,这可以通过构建时的反射来完成。)
我建议查看内置的序列化支持< /a> 在推出您自己的版本之前...
You can use GetField instead of
GetProperty
if you want to retrieve a field.It is definitely expensive, but I'd profile it to determine if you will have a performance issue.
The other main option(s) is to build some form of dictionary yourself that maps "names" to values. This can be a direct dictionary mapping to the value, or a dictionary that maps to a delegate which retrieves the value. This does have the advantage in that you can make it work the same way for properties or fields, but each class needs to create the mapping. (This can, however, potentially be done via reflection at construction time.)
I would recommend reviewing the built-in Serialization Support prior to rolling your own version...
如果您使用 C# 4.0(与 .NET Framework 4.0 和 Visual Studio 2010 一起发布),则新的
dynamic
关键字将简化代码中的内容:等等。我的理解是,这会做更多事情- 或者说,在幕后使用反射和测试是否存在兼容的
PropertyInfo
/MethodInfo
/etc 是一样的。然后调用它们,因此性能仍然是一个问题,但它的可读性肯定要高得多。If you're using C# 4.0 (released alongside the .NET Framework 4.0 and Visual Studio 2010), there's the new
dynamic
keyword that will simplify things in your code:etc. My understanding is that this does more-or-less the same thing under the hood as using reflection and testing for the existence of compatible
PropertyInfo
/MethodInfo
/etc. and then invoking them, so performance is still a concern, but it's certainly a lot more readable.我对缓存的想法,欢迎评论。
What I came up with for caching, comments welcome.