【问题标题】:Enum GetCustomAttributes does not show anything枚举 GetCustomAttributes 不显示任何内容
【发布时间】:2021-10-20 22:48:54
【问题描述】:

在这个简单的类中,我尝试覆盖 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;
    }
}

它没有显示任何值或错误。

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

【问题讨论】:

标签: c#


【解决方案1】:

属性是这样的:

[MyCoolAttribute]
public MyCoolMethod()

您正在寻找的是属性

this.GetType().GetProperties()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-29
    • 2020-11-10
    • 2012-04-12
    • 2019-11-11
    • 2017-09-25
    • 2012-08-02
    • 2019-08-11
    • 2018-06-25
    相关资源
    最近更新 更多