.NET 4.0 中的 GetCustomAttribute 等效项
GetCustomAttribute Equivalent in .NET 4.0
我正在使用来自 here
的 Entity Framework Audit Trail 示例项目
问题是我们已经在项目中使用 .NET 4.0,但此示例使用的是 .NET 4.5。
有人能告诉我 .NET 4 中 GetCustomAttribute
的等价物是什么,
下面是我的代码:
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;
}
在此代码中:GetCustomAttribute
未被识别。
MemberInfo.GetCustomAttribute<T>()
belongs to the CustomAttributeExtensions
extension class which contains very thin wrappers to Attribute.GetCustomAttribute()
and Attribute.GetCustomAttributes()
. These wrappers cast the returned Attribute
to the expected attribute type. See the reference source here: https://referencesource.microsoft.com/#mscorlib/system/reflection/CustomAttributeExtensions.cs.
我正在使用来自 here
的 Entity Framework Audit Trail 示例项目问题是我们已经在项目中使用 .NET 4.0,但此示例使用的是 .NET 4.5。
有人能告诉我 .NET 4 中 GetCustomAttribute
的等价物是什么,
下面是我的代码:
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;
}
在此代码中:GetCustomAttribute
未被识别。
MemberInfo.GetCustomAttribute<T>()
belongs to the CustomAttributeExtensions
extension class which contains very thin wrappers to Attribute.GetCustomAttribute()
and Attribute.GetCustomAttributes()
. These wrappers cast the returned Attribute
to the expected attribute type. See the reference source here: https://referencesource.microsoft.com/#mscorlib/system/reflection/CustomAttributeExtensions.cs.