How do I access an object property with a string variable that has the name of that property?

后端 未结 2 1395
一个人的身影
一个人的身影 2021-01-02 09:27

How do I do this in C#?

using System;

namespace TestProperties28373
{
    class Program
    {
        static void Main(string[] args)
        {
                     


        
相关标签:
2条回答
  • 2021-01-02 09:48

    Use reflection :

    using System.Reflection;
    
    ...
    
    PropertyInfo prop = typeof(Customer).GetProperty(propertyName);
    object value = prop.GetValue(customer, null);
    
    0 讨论(0)
  • 2021-01-02 10:03

    Use System.Reflection and PropertyInfo

    0 讨论(0)
提交回复
热议问题