Type.GetProperties不返回任何属性

use*_*295 0 c# properties

我正在尝试遍历类中的每个属性,输出属性的名称和值.但是我的代码没有返回任何属性.

通过以下方式循环:

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;
}
Run Code Online (Sandbox Code Playgroud)

用于遍历所有代码的代码:

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));
}
Run Code Online (Sandbox Code Playgroud)

然而,当我改变public ushort Fps;public ushort Fps { get; set; }它会找到它.

Ale*_*exD 10

public ushort Fps;
public ushort Height;
...
Run Code Online (Sandbox Code Playgroud)

它们不是属性而是字段.试试吧GetFields.或者,可能更好,将它们转换为属性.例如

public ushort Fps {get; set;}
public ushort Height {get; set;}
Run Code Online (Sandbox Code Playgroud)