GetCustomAttribute在.NET 4.0中等效

Usm*_*lid 5 .net c# reflection .net-4.0

我从这里使用Entity Framework Audit Trail的示例项目

问题是我们已在项目中使用.NET 4.0,但此示例使用的是.NET 4.5.

有人可以告诉我GetCustomAttribute.NET 4中的等价物,下面是我的代码:

    private static string ColumnNameFactory(this Type type, string propertyName)
    {
        string columnName = propertyName;
        Type entityType = type.GetEntityType();
        var columnAttribute = entityType.GetProperty(propertyName).GetCustomAttribute<ColumnAttribute>(false);
        if (columnAttribute != null && !string.IsNullOrEmpty(columnAttribute.Name))
        {
            columnName = columnAttribute.Name;
        }
        return columnName;
    }
Run Code Online (Sandbox Code Playgroud)

在此代码中:GetCustomAttribute无法识别.

dbc*_*dbc 7

MemberInfo.GetCustomAttribute<T>()属于CustomAttributeExtensions扩展类,它包含非常薄的包装器Attribute.GetCustomAttribute()Attribute.GetCustomAttributes().这些包装器将返回的内容强制Attribute转换为预期的属性类型.请参阅参考资料来源:https://referencesource.microsoft.com/#mscorlib/system/reflection/CustomAttributeExtensions.cs.