C# Dynamic delegate for getter and setter of dynamically created class using reflection

  • Thread starter Thread starter Harihara Krishnan
  • Start date Start date
H

Harihara Krishnan

Guest
I am using this .Net reflection TypeBuilder class to generate a class instance at runtime.

I am using .Net reflections to create list of objects with dynamic set of properties since I am reading input from excel file which may have dynamic columns as per the business requirement. But I am doing lot of loops to get the GetType().GetProperty("") which is reducing the performance. I am trying to delegate it dynamically for the PropertiesInfo[] which I get from the GetType().GetProperties().

Below is a static getter and setter delegate for Property1 of the runtime class create

Action<MyClass, int> setter = (Action<MyClass, int>)Delegate.CreateDelegate(typeof(Action<MyClass, int>), null, typeof(MyClass).GetProperty("Property1").GetSetMethod());


I would like to make this dynamic for each property created of my class. I'm stuck and not sure whether I can use any Linq MemberExpression to achieve it.


Can anyone help me out? That would be great.

Continue reading...
 
Back
Top