如何从泛型方法访问类的属性–C#我有一个具有以下属性的三级类ClassA{publicint客户ID{得到;放;}公共字符串名称{得到;放;}}B类{publicintCustID{get;放;}公共字符串年龄{得到;publicvoidProceesData(IListparam1,stringdate1){Parallel.ForEach(T,(currentItem)=>{//我想访问param1的CustID属性并将该值传递给另一个函数GetDetails(CustID);RaiseRequest(param1);});CustID属性存在于两个类中(即在类A和类B中)。如何在此泛型方法中访问CustID属性?任何人都可以帮助解决这个问题另一种可能性是使用System.Reflection。使用该PropertyInfo从给定类型T获取具有PropertyInfo名称的PropertyInfo,您可以使用GetValue获取该属性的相应值。这里有一个小测试程序来说明:publicclassClassA{publicintCustID{get;放;}公共字符串名称{得到;放;}}publicclassClassB{publicintCustID{get;放;}公共字符串年龄{得到;放;}}publicstaticvoidProceesData(IListparam1,stringdate1){Parallel.ForEach(param1,(currentItem)=>{//我想访问param1的CustID属性并将该值传递给另一个函数varvalue=typeof(T).GetProperty("CustID").GetValue(currentItem);Console.WriteLine("Value:"+value);});}publicstaticvoidMain(string[]args){列表测试=newList();测试。添加(新ClassA{CustID=123});测试。添加(新ClassA{CustID=223});测试。添加(新ClassA{CustID=323});ProceesData(测试,“测试”);}编辑为了使它更通用,您可以将参数名称传递给该方法:param1的CustID属性并将该值传递给另一个函数varvalue=typeof(T).GetProperty(参数).GetValue(currentItem);Console.WriteLine("值:"+值);});现在您可以决定使用哪个参数:ProceesData(test,"test","Name");或ProceesData(测试,“测试”,“年龄”);正如Gusman所建议的那样,您可以通过在循环之前仅获取一次PropertyInfo来加快速度:PropertyInfopi=typeof(T).GetProperty(parameter);Parallel.ForEach(param1,(currentItem)=>{//我想访问param1的CustID属性并将该值传递给另一个函数varvalue=pi.GetValue(currentItem);Console.WriteLine("Value:"+value);});编辑显然性能似乎是一个问题所以这里有一个比较。如果您有时间等待,您可以自己尝试一下。如果我们测量物业的访问时间:publicstaticvoidProceesDataD(IListparam1,stringdate1){Parallel.ForEach(param1,(currentItem)=>{dynamicobj=currentItem;intcustId=obj.CustID;});}publicstaticvoidProceesData(IListparam1,stringdate1)whereT:ICust{Parallel.ForEach(param1,(currentItem)=>{varvalue=currentItem.CustID;});}publicstaticvoidProceesData(IListparam1,stringdate1,stringparameter){PropertyInfopi=typeof(T).GetProperty(parameter);Parallel.ForEach(param1,(currentItem)=>{varvalue=pi.GetValue(currentItem);});}publicstaticvoidMain(string[]args){列表测试=newList();列出testA=newList();秒表st=new秒表();对于(inti=0;i(test,"test","CustID");st.Stop();Console.WriteLine("Reflection:"+st.ElapsedMilliseconds);st.Restart();ProceesData(testA,“测试”);st.Stop();Console.WriteLine(“接口:”+st.ElapsedMilliseconds);st.Restart();ProceesDataD(测试,“测试”);st.Stop();Console.WriteLine("动态:"+st.ElapsedMilliseconds);免责声明:使用代码片段一次只测量一次不要按原样运行程序,而是每次单独测试。介绍接口:interfaceICust{publicintCustID{get;}}classA:ICust{publicintCustID{get;放;}公共字符串名称{得到;放;}}B类:ICust{publicintCustID{get;放;}公共字符串年龄{得到;放;}}publicvoidProceesData(IListparam1,stringdate1)whereT:ICust{Parallel.ForEach(param1,(currentItem)=>{GetDetails(currentItem.CustID)});如果您不能在现有类上引入接口或基类,另一种方法是使用动态:publicvoidProceesData(IListparam1,stringdate1){Parallel.ForEach(param1,(currentItem)=>{dynamicobj=currentItem;intcustId=obj.CustID;});}继承将起作用publicabstractclassABBase{publicintCustID{gete;放;}}publicclassA:ABBase{publicstringName{get;放;}}publicclassB:ABBase{publicstringAge{get;放;}}然后使用代替泛型方法更多关于C#学习教程,希望大家多多关注—publicvoidProcessData(IListparam1,stringdate){Parallel.ForEach(T,(currentItem)=>{GetDetails(CustID)});}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
