Using Reflection and Expression to get Property with better performance.
2015-03-01 22:29
483 查看
I wrote a PropertyAccessor class for getting a property from a class using Reflection, yet it’s having better performance.
public class PropertyAccessor<TObject> { private Func<TObject, object> getter; private Action<TObject, object> setter; public PropertyAccessor(string propertyName) { var propertyInfo = typeof(TObject).GetProperty(propertyName); if (null != propertyName) { var getMethodInfo = propertyInfo.GetGetMethod(); if (null != getMethodInfo) { var instance = Expression.Parameter(propertyInfo.DeclaringType, propertyName); var property = Expression.Property(instance, propertyInfo); // boxing/unboxing var convert = Expression.TypeAs(property, typeof(object)); getter = (Func<TObject, object>)Expression.Lambda(convert, instance).Compile(); } var setMethodInfo = propertyInfo.GetSetMethod(); if (null != setMethodInfo) { var instance = Expression.Parameter(propertyInfo.DeclaringType, propertyName); var argument = Expression.Parameter(typeof(object), propertyName); var setterCall = Expression.Call( instance, propertyInfo.GetSetMethod(), // boxing/unboxing Expression.Convert(argument, propertyInfo.PropertyType)); setter = (Action<TObject, object>)Expression.Lambda(setterCall, instance, argument).Compile(); } } } public object GetValue(TObject obj) { // TODO; exception handling return getter(obj); } public void SetValue(TObject obj, object newValue) { // TODO: exception handling setter(obj, newValue); } }
相关文章推荐
- How to get the password text in a text with password property from another process using C++ - 用C++如何从不同进程获取密码框文本
- How to get Expression Encoder to work with the latest Apple QuickTime (7.64.17.0 and greater)
- How-To : Using EJB 3.0 and Java Persistence API with Spring in OC4J
- CSV ----- To improve the performance of Excel Output with reflection (Part 1)
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- 项目管理实践【四】自动编译和发布网站【Using Visual Studio with Source Control System to build and publish website automatically】
- Using Visual Basic and ASP with XML to Generate Excel 2003 Workbooks
- A Data Access Layer to persist business objects using attributes and reflection - Part II [无常译]
- Using partition by to write alternative SQL to get effdt and effseq items
- How to configure XDB for using ftp and http protocols with ASM [ID 357714.1]
- 项目管理实践【五】自动编译和发布网站【Using Visual Studio with Source Control System to build and publish website autom
- Effective IT Project Management: Using Teams to Get Projects Completed on Time and Under Budget
- Many ways to communicate with your database using jQuery AJAX and ASP.NET
- Photoshop CS2 RAW : Using Adobe Camera Raw, Bridge, and Photoshop to Get the Most out of Your Digita
- (solved) nvidia- how to get better resolution with nouveau?
- A Data Access Layer to persist business objects using attributes and reflection - Part III
- 项目管理实践【五】自动编译和发布网站【Using Visual Studio with Source Control System to build and publish website automatically】
- 项目管理实践【五】自动编译和发布网站【Using Visual Studio with Source Control System to build and publish website automatically】
- A Data Access Layer to persist business objects using attributes and reflection - Part I [无常译]
- How to configure XDB for using ftp and http protocols with ASM [ID 357714.1]