枚举 GetCustomAttributes 不显示任何内容

use*_*793 0 c#

在这个简单的类中,我尝试覆盖 ToString() 以显示所有自定义属性。

public class TryMe
{
    public TryMe(int id, List<String> tests)
    {
        ID = id;
        Tests = tests;
    }
    public int ID { get; set; }
    public List<String> Tests { get; set; }

    public override string ToString()
    {
        string me = "";
        var attributes = this.GetType().GetCustomAttributes();

        foreach (var attribute in attributes)
        {
            me = me + attribute.ToString() + ",";
        }
        return me;
    }
}
Run Code Online (Sandbox Code Playgroud)

它不显示任何值或错误。

不是IDTests自定义属性吗?如果类变大,是否有任何简单的枚举?

fas*_*aso 5

属性是这样的:

[MyCoolAttribute]
public MyCoolMethod()
Run Code Online (Sandbox Code Playgroud)

您正在寻找的是属性

this.GetType().GetProperties()