Use reflection to get the value of a property by name in a class instance

后端 未结 4 1769
[愿得一人]
[愿得一人] 2020-12-19 03:12

Lets say I have

class Person
{
    public Person(int age, string name)
    {
        Age = age;
        Name = name; 
    }
    public int Age{get;set}
             


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 03:38

    I think this is the proper syntax...

    var myPropInfo = myType.GetProperty("MyProperty");
    var myValue = myPropInfo.GetValue(myInstance, null);
    

提交回复
热议问题