[英]GetCustomAttribute Equivalent in .NET 4.0
我从这里使用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>()
属于CustomAttributeExtensions
扩展类,它包含Attribute.GetCustomAttribute()
和Attribute.GetCustomAttributes()
非常薄的包装器。 这些包装器将返回的Attribute
转换为期望的属性类型。 请参阅参考资料来源: https : //referencesource.microsoft.com/#mscorlib/system/reflection/CustomAttributeExtensions.cs 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.