2013-05-31 51 views
3

嗨,這裏是我正在嘗試做的事情,我有一個類(EventType),它可以是動態的,並在不同時間具有不同的成員/屬性。在運行時使用反射來獲取類的實例的屬性C#

class EventType 
{ 
    int id{set;} 
    string name{set;} 
    DateTime date{set;} 
    List<int> list{set;} 
    Guid guid{set;} 
} 

在我的主要方法,我在這個類中傳遞一個實例給一個函數在不同的類,並試圖反思來獲得實例,但它不是成功的特性,給我空值。

class Program 
{ 
    static void Main(string[] args) 
    { 
     EventType event1 = new EventType(); 
     int rate = 100; 
     DataGenerator.Generate<EventType>(event1, rate); 
    } 
    public static byte[] test(EventType newEvent) 
    { 
     return new byte[1]; 
    } 
} 



static class DataGenerator 
{ 
    public static void Generate<T>(T input, int eventRate, Func<T, byte[]> serializer=null) 
    {    
     Type t = input.GetType(); 
     PropertyInfo[] properties = t.GetProperties(); 
     foreach (PropertyInfo property in properties) 
     { 
      Console.WriteLine(property.ToString()); 
     } 
     var bytes = serializer(input); 
    } 
} 
+0

關於你提到的「生成」的方法,我想知道什麼是不同外的開箱系列化? (二進制序列化,xml序列化或JSon)。你不應該依賴現有的序列化機制嗎? –

+0

我試圖讓人們可以使用自己的序列化程序的方法,如果他們不提供一個我會繼續使用現有的一個 – MJSHARMA

回答

3

您的類屬性默認爲私有,並且GetProperties只返回公共屬性。

無論是宣傳自己的屬性市民:

class EventType 
{ 
    public int id{set;} 
    public string name{set;} 
    public DateTime date{set;} 
    public List<int> list{set;} 
    public Guid guid{set;} 
} 

或者指定綁定FLAS獲得非公開性質:

Type t = input.GetType(); 
PropertyInfo[] properties = t.GetProperties(
    BindingFlags.NonPublic | // Include protected and private properties 
    BindingFlags.Public | // Also include public properties 
    BindingFlags.Instance // Specify to retrieve non static properties 
    ); 
+0

@peter感謝您的幫助將是什麼情況如果我使用字段而不是屬性? 'code' class EventType { int id; 字符串名稱; DateTime date; 列表列表; Guid guid; }'code' – MJSHARMA

+0

用''GetFields''替代'GetProperties'(http://msdn.microsoft.com/en-us/library/6ztex2dc.aspx) –

1

Type.GetProperties全部返回public屬性,在你的班級裏他們都是私人的,所以他們不會被退回。您可以讓他們公開或使用BindingFlags搜索私立學校也,如:

t.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance); 
1
class EventType 
{ 
    int id { get; set; } 
    string name { get; set; } 
    DateTime date { get; set; } 
    List<int> list { get; set; } 
    Guid guid { get; set; } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     EventType event1 = new EventType(); 
     int rate = 100; 
     DataGenerator.Generate<EventType>(event1, rate); 
    } 
    public static byte[] test(EventType newEvent) 
    { 
     return new byte[1]; 
    } 
} 



static class DataGenerator 
{ 
    public static void Generate<T>(T input, int eventRate, Func<T, byte[]> serializer = null) 
    { 
     Type t = input.GetType(); 
     PropertyInfo[] properties = t.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); 
     foreach (PropertyInfo property in properties) 
     { 
      Console.WriteLine(property.ToString()); 
     } 
     //var bytes = serializer(input); 
    } 
} 
+0

'code'感謝您的幫助將會是什麼情況如果我使用字段而不是屬性?class EventType { int id; 字符串名稱; DateTime date; 列表列表; Guid guid; '代碼' – MJSHARMA

相關問題