如何使用具有该属性名称的字符串变量访问对象属性?我如何在C#中执行此操作?使用系统;namespaceTestProperties28373{classProgram{staticvoidMain(string[]args){Customercustomer=newCustomer{FirstName="Jim",LastName="Smith",Age=34};Console.WriteLine(customer.FirstName);stringpropertyName="名字";Console.WriteLine(客户.&&propertyName);//伪代码Console.ReadLine();}}classCustomer{publicstringFirstName{get;放;}publicstringLastName{get;放;}publicintAge{得到;放;}}}使用反射:使用System.Reflection;...PropertyInfoprop=typeof(Customer).GetProperty(propertyName);对象值=prop.GetValue(customer,null);使用System.Reflection和PropertyInfo以上是C#学习教程:如何使用带有属性名的字符串变量来访问对象属性?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
