我想循环遍历类中的每个属性,输出属性的名称和值。但我的代码没有返回任何属性。Type.GetProperties不返回任何属性
类绕环通过:通过他们全部用来循环
public class GameOptions
{
public ushort Fps;
public ushort Height;
public ushort Width;
public bool FreezeOnFocusLost;
public bool ShowCursor;
public bool StaysOnTop;
public bool EscClose;
public string Title;
public bool Debug;
public int DebugInterval = 500;
}
代码:
foreach (PropertyInfo property in this.Options.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
debugItems.Add("Setting Name: " + property.Name);
debugItems.Add("Setting Value: " + property.GetValue(this,null));
}
但是当我改变public ushort Fps;
到public ushort Fps { get; set; }
它会找到它。
怎么就认定他们当我添加{get;组; } 然后?还有另一种方式来循环他们所有? – user1763295
@ user1763295你让他们属性:)。 – AlexD
谢谢,它工作!我会尽可能接受你的答案。 – user1763295