Is there a faster way to access a property member of a class using reflection?

Posted by Ross Goddard on Stack Overflow See other posts from Stack Overflow or by Ross Goddard
Published on 2010-12-22T18:45:49Z Indexed on 2010/12/22 19:54 UTC
Read the original article Hit count: 178

Filed under:
|

I am currently using the following code to access the property of an object using reflection:

 Dim propInfo As Reflection.PropertyInfo = myType.GetProperty(propName)
 Dim objValue As Object = propInfo.GetValue(myObject, Nothing)

I am having some issues with the speed since this type of code is being called many times and is causing some slowdown.

I have been looking into using Refelction.Emit or dynamic methods, but I am not sure exactly how to make use of them.

Background Information: I am creating a list of a subset of the properties of the object, associating then with some meta information (such as if they can be loaded from the database or xml, if they are editable, can the user see them). This is for later consumption so we can write code such as :

foreach prop as BaseWrapper in graphNode.NodeProperties
    prop.LoadFromDataRow(dr)
next

The application makes heavy use of having access to this list. The problem is that on the initial load of a project, a larger number of objects are being created that make use of this, so for each object created it is looping through this code a number of times. I initially tried adding each property to the list manually, but this ran into problems with not everything being initialized at the correct time and some other issues.

If there is no other good way, then I may have to rethink some of the design and see what else can be done to improve the performance.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about reflection