为什么获取私有属性什么也得不到? (C#)
Why does it get nothing that Getting private Properties? (c#)
public class A{
public Info m_Info = new Info();
Main()
{
Console.WriteLine(m_Info.Property_Count());
}
public class Info{
protected int i_Id;
protected string s_Name;
public int Property_Count(){
System.Reflection.PropertyInfo[] data = this.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
foreach(var item in data)
Console.WriteLine(item.Name);
return data.Length;
}
}
}
我正在尝试获取私有或受保护的属性~
但是 data.Length 总是 return 0,听起来好像什么也得不到,为什么?
您的 class 没有 properties。这些:
protected int i_Id;
protected string s_Name;
是 fields。您可以将它们更改为属性:
protected int i_Id { get; set; }
protected string s_Name { get; set; }
或者您可以使用 GetFields
:
System.Reflection.FieldInfo[] data = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
public class A{
public Info m_Info = new Info();
Main()
{
Console.WriteLine(m_Info.Property_Count());
}
public class Info{
protected int i_Id;
protected string s_Name;
public int Property_Count(){
System.Reflection.PropertyInfo[] data = this.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
foreach(var item in data)
Console.WriteLine(item.Name);
return data.Length;
}
}
}
我正在尝试获取私有或受保护的属性~ 但是 data.Length 总是 return 0,听起来好像什么也得不到,为什么?
您的 class 没有 properties。这些:
protected int i_Id;
protected string s_Name;
是 fields。您可以将它们更改为属性:
protected int i_Id { get; set; }
protected string s_Name { get; set; }
或者您可以使用 GetFields
:
System.Reflection.FieldInfo[] data = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);