小编moo*_*per的帖子

从对象获取自定义属性

当我尝试从object函数返回时获取自定义属性null.为什么?

class Person
{
    [ColumnName("first_name")]
    string FirstName { get; set; }

    Person()
    {
        FirstName = "not important";
        var attrs = AttributeReader.Read(FirstName);
    }
}

static class AttributeReader
{
    static object[] Read(object column)
    {
        return column.GetType().GetCustomAttributes(typeof(ColumnNameAttribute), false);
    }
}
Run Code Online (Sandbox Code Playgroud)

c# reflection attributes custom-attributes

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

attributes ×1

c# ×1

custom-attributes ×1

reflection ×1