通过反射获取值,没有垃圾
我正在编写一个系统,它要求我获取对象中的属性值,最好使用反射。这个项目是针对 xbox360 的,它在紧凑的框架上运行,因此有一个缓慢的垃圾收集器 - 这意味着我避免分配是绝对重要的!
我发现做到这一点的唯一方法是:
Foo Something; //an object I want to get data from
PropertyInfo p; //get this via reflection for the property I want
object value = p.GetGetmethod().Invoke(Something, null);
//Now I have to cast value into a type that it should be
我不喜欢这个有两个原因:
- 铸造是为陶工准备的,泛型是为程序员准备的。
- 显然,每次我必须获取原始值并且它被装箱时,它都会产生垃圾。
是否有一些通用方法可以从属性中获取值,并且不会对基元进行装箱?
编辑::为了回应 Jons 的回答,从他的博客中窃取的代码不会导致分配,问题已解决:
String methodName = "IndexOf";
Type[] argType = new Type[] { typeof(char) };
String testWord = "TheQuickBrownFoxJumpedOverTheLazyDog";
MethodInfo method = typeof(string).GetMethod(methodName, argType);
Func<char, int> converted = (Func<char, int>)Delegate.CreateDelegate
(typeof(Func<char, int>), testWord, method);
int count = GC.CollectionCount(0);
for (int i = 0; i < 10000000; i++)
{
int l = converted('l');
if (GC.CollectionCount(0) != count)
Console.WriteLine("Collect");
}
I am writing a system which requires me to fetch the values of properties in an object, preferably using reflection. This project is for the xbox360, which runs on the compact framework and thus has a slow garbage collector - this means it's absolutely vital that I avoid allocations!
The only way I have found to do this is:
Foo Something; //an object I want to get data from
PropertyInfo p; //get this via reflection for the property I want
object value = p.GetGetmethod().Invoke(Something, null);
//Now I have to cast value into a type that it should be
I dislike this for 2 reasons:
- Casting is for potters, generics is for programmers
- It obviously creates garbage every time I have to get a primitive value and it gets boxed.
Is there some generic method for getting the value from a property, which will not box primitives?
EDIT:: In response to Jons answer, this code stolen from his blog does not cause allocations, problem solved:
String methodName = "IndexOf";
Type[] argType = new Type[] { typeof(char) };
String testWord = "TheQuickBrownFoxJumpedOverTheLazyDog";
MethodInfo method = typeof(string).GetMethod(methodName, argType);
Func<char, int> converted = (Func<char, int>)Delegate.CreateDelegate
(typeof(Func<char, int>), testWord, method);
int count = GC.CollectionCount(0);
for (int i = 0; i < 10000000; i++)
{
int l = converted('l');
if (GC.CollectionCount(0) != count)
Console.WriteLine("Collect");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种替代方法是使用
Delegate 从 getter 方法创建委托。 CreateDelegate
- 我不知道 Xbox 使用的紧凑框架版本是否支持它。我有一个 博客在
Delegate.CreateDelegate
上发布,您可能会发现它很有用 - 但同样,您需要了解其中有多少内容适用于 Xbox。One alternative would be to create a delegate from the getter method using
Delegate.CreateDelegate
- I don't know whether that's supported on the compact framework version used by the Xbox though.I have a blog post on
Delegate.CreateDelegate
which you may find useful - but again, you'll need to see how much of it is applicable to the Xbox.