C#反思:更新属性值最快的方法? [英] C# Reflection: Fastest Way to Update a Property Value?

查看:371
本文介绍了C#反思:更新属性值最快的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是更新使用反射属性的最快方法?假设属性始终是一个int:

 的PropertyInfo counterPropertyInfo =的getProperty(); 
int值=(int)的counterPropertyInfo.GetValue(本,NULL);
counterPropertyInfo.SetValue(这一点,值+ 1,NULL);


解决方案

只是要确保你有某种缓存的PropertyInfo,所以,你是不是重复地调用type.GetProperty。除此之外,它很可能会更快,如果你创建了一个委托上执行的增量,还是像泰奥曼建议做类型实现的接口,并使用该类型的方法。


Is this the fastest way to update a property using reflection? Assume the property is always an int:

PropertyInfo counterPropertyInfo = GetProperty();
int value = (int)counterPropertyInfo.GetValue(this, null);
counterPropertyInfo.SetValue(this, value + 1, null);

解决方案

Just be sure that you are caching the PropertyInfo somehow, so that you aren't repeatably calling type.GetProperty. Other than that it would probably be faster if you created a delegate to a method on the type that performed the increment, or like Teoman suggested make the type implement an interface and use that.

这篇关于C#反思:更新属性值最快的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆