2009-05-28 5 views
9

मैं इसे सी # में कैसे कर सकता हूं?मैं किसी ऑब्जेक्ट प्रॉपर्टी को स्ट्रिंग वैरिएबल से कैसे एक्सेस करूं जिसमें उस संपत्ति का नाम हो?

using System; 

namespace TestProperties28373 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Customer customer = new Customer { FirstName = "Jim", LastName = "Smith", Age = 34}; 

      Console.WriteLine(customer.FirstName); 

      string propertyName = "FirstName"; 
      Console.WriteLine(customer.&&propertyName); //PSEUDO-CODE 

      Console.ReadLine(); 

     } 
    } 

    class Customer 
    { 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public int Age { get; set; } 
    } 
} 

उत्तर

20

उपयोग प्रतिबिंब:

using System.Reflection; 

... 

PropertyInfo prop = typeof(Customer).GetProperty(propertyName); 
object value = prop.GetValue(customer, null); 
संबंधित मुद्दे

 संबंधित मुद्दे