2009-08-30 13 views
20

Statik bir kurucu kullanarak, tüm özelliklerin statik bir dizisini oluşturan bir sınıfa sahibim. Ayrıca, bu dizideki her özelliğin & türünü listeleyen GetNamesAndTypes() işlevine de sahibim. GetNamesAndTypesAndValues ​​() - - adı & sınıftaki her özelliğin türünü yanı sıra bu örneğinin değeri görüntülerPropertyInfo.GetValue() öğesini kullanma

Şimdi başka örnek düzeyi fonksiyonunu oluşturmak istiyorum. Bunu nasıl yaparım?

//StaticTest.cs 
using System; 
using System.ComponentModel; 
using System.Globalization; 
using System.Reflection; 

namespace StaticTest 
{ 
    public class ClassTest 
    { 
     private string m_A, m_B, m_C; 
     private static PropertyInfo[] allClassProperties; 

     static ClassTest() 
     { 
      Type type = typeof(ClassTest); 
      allClassProperties = type.GetProperties(); 

      // Sort properties alphabetically by name 
      // (http://www.csharp-examples.net/reflection-property-names/) 
      Array.Sort(allClassProperties, delegate(PropertyInfo p1, PropertyInfo p2) 
      { 
       return p1.Name.CompareTo(p2.Name); 
      }); 
     } 

     public int A 
     { 
      get { return Convert.ToInt32(m_A); } 
      set { m_A = value.ToString(); } 
     } 

     public string B 
     { 
      get { return m_B; } 
      set { m_B = value; } 
     } 

     public DateTime C 
     { 
      get { return DateTime.ParseExact("yyyyMMdd", m_C, 
            CultureInfo.InvariantCulture); } 
      set { m_C = String.Format("{0:yyyyMMdd}", value); } 
     } 

     public static void GetNamesAndTypes() 
     { 
      foreach (PropertyInfo propertyInfo in allClassProperties) 
      { 
       Console.WriteLine("{0} [type = {1}]", propertyInfo.Name, 
              propertyInfo.PropertyType); 
      } 
     } 

     public void GetNamesAndTypesAndValues() 
     { 
      foreach (PropertyInfo propertyInfo in allClassProperties) 
      { 
       Console.WriteLine("{0} [type = {1}]", propertyInfo.Name, 
              propertyInfo.PropertyType); 
      } 
     } 
    } 
} 

//Program.cs 
using System; 
using System.Collections.Generic; 
using StaticTest; 

namespace ConsoleApplication2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("[static] GetNamesAndTypes()"); 
      ClassTest.GetNamesAndTypes(); 
      Console.WriteLine(""); 

      ClassTest classTest = new ClassTest(); 
      classTest.A = 4; 
      classTest.B = @"bacon"; 
      classTest.C = DateTime.Now; 
      Console.WriteLine("[instance] GetNamesAndTypesAndValues()"); 
      classTest.GetNamesAndTypesAndValues(); 

      Console.ReadLine(); 
     } 
    } 
} 

Ben PropertyInfo.GetValue() kullanarak denedi, ama işe alamadım: Ben bugüne kadar yazdım kod.

cevap

33

Örneğinizde propertyInfo.GetValue(this, null) çalışmalıdır. aşağıdaki gibi GetNamesAndTypesAndValues() değiştirerek düşünün:

public void GetNamesAndTypesAndValues() 
{ 
    foreach (PropertyInfo propertyInfo in allClassProperties) 
    { 
    Console.WriteLine("{0} [type = {1}] [value = {2}]", 
     propertyInfo.Name, 
     propertyInfo.PropertyType, 
     propertyInfo.GetValue(this, null)); 
    } 
} 
+11

ince iken, normal basit özellikler için, bu PropertyInfo.GetIndexParameters belirtildiği gibi boş olmayan bir argüman listesi almak dizin özellikleri ile başarısız olur. –

İlgili konular