C#使用反射的动态创建类的getter和setter的动态委托 [英] C# Dynamic delegate for getter and setter of dynamically created class using reflection
问题描述
我正在使用这个.Net反射TypeBuilder类在运行时生成一个类实例。
我正在使用.Net反射来创建具有动态属性集的对象列表,因为我正在从excel文件中读取输入,该文件可能具有根据业务需求的动态列。但是我做了很多循环来得到 GetType()。GetProperty("")
其中
正在降低性能。我正在尝试动态委托它为 PropertiesInfo []
其中
来自 GetType()。 GetProperties()
。
下面是运行时类的Property1的静态getter和setter委托create
Action< MyClass,int> setter =(Action< MyClass,int>)Delegate.CreateDelegate(typeof(Action< MyClass,int>),null,typeof(MyClass).GetProperty(" ; Property1")。GetSetMethod());
我想为我班级创建的每个属性创建动态。我卡住了,不知道我是否可以使用任何Linq
MemberExpression
实现它。
有谁可以帮帮我?那会很棒。
Hi Harihara Krishnan,
感谢您在此发帖。
对于您的问题,我提供了以下代码。
1。我通过PropertyInfo获取了类中的所有属性并将它们存储在一个数组中。
2.我使用foreach循环迭代数组中的属性,并且属性创建一个委托。
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Reflection;
使用System.Text;
使用System.Threading.Tasks;
名称空间test1
{
公共类MyClass
{
私人字符串名称;
公共字符串姓名
{
获得
{
返回姓名;
}
设置
{
name = value;
}
}
公共字符串ID
{
get
{
返回id;
}
set
{
id = value;
}
}
private string id;
}
class Program
{
static void Main(string [] args)
{
类型t = typeof(MyClass);
PropertyInfo [] properties = t.GetProperties();
foreach(属性中的var项目)
{
操作< string> setter =(Action< string>)Delegate.CreateDelegate(typeof(Action< string>),null,typeof(MyClass).GetProperty(item.Name).GetSetMethod());
}
Console.ReadKey();
}
}
}
祝你好运,
Jack J Jun。 跨度>
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.
Hi Harihara Krishnan ,
Thank you for posting here.
For your question, I made the following code.
1. I got all the properties in the class through PropertyInfo and stored them in an array.
2. I use a foreach loop to iterate through the properties in the array, and a property creates a delegate.
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace test1 { public class MyClass { private string name; public string Name { get { return name; } set { name = value; } } public string ID { get { return id; } set { id = value; } } private string id; } class Program { static void Main(string[] args) { Type t = typeof(MyClass); PropertyInfo[] properties = t.GetProperties(); foreach (var item in properties) { Action<string> setter = (Action<string>)Delegate.CreateDelegate(typeof(Action<string>), null, typeof(MyClass).GetProperty(item.Name).GetSetMethod()); } Console.ReadKey(); } } }
Best regards,
Jack J Jun.
这篇关于C#使用反射的动态创建类的getter和setter的动态委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!