2013-05-31 53 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

相关问题