问题描述
有人告诉我使用Reflection.Emit而不是PropertyInfo.GetValue / SetValue,因为这样可以更快。
但是我真的不知道Reflection.Emit有什么功能,以及如何用它替代GetValue和SetValue。有人可以帮我吗?
I've been told to use Reflection.Emit instead of PropertyInfo.GetValue / SetValue because it is faster this way.But I don't really know what stuff from Reflection.Emit and how to use it to substitute GetValue and SetValue. Can anybody help me with this ?
推荐答案
只是一个替代答案;如果您想要性能,但需要类似的API,请考虑;它在下面使用了 Reflection.Emit
(因此您不必这样做),但在 PropertyDescriptor
API中公开了自己,因此您可以使用:
Just an alternative answer; if you want the performance, but a similar API - consider HyperDescriptor; this uses Reflection.Emit
underneath (so you don't have to), but exposes itself on the PropertyDescriptor
API, so you can just use:
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
props["Name"].SetValue(obj, "Fred");
DateTime dob = (DateTime)props["DateOfBirth"].GetValue(obj);
启用一行代码,并处理所有缓存等。
One line of code to enable it, and it handles all the caching etc.
这篇关于反射。比GetValue&设定值:S的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!