Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetCustomAttribute Equivalent in .NET 4.0

I am using a sample project for Entity Framework Audit Trail from here

The problem is we are already using .NET 4.0 in our project but this sample is using .NET 4.5.

Can somebody tell me what is the equivalent of GetCustomAttribute in .NET 4, Below is my code:

    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;
    }

In this code: GetCustomAttribute is not recognized.

like image 603
Usman Khalid Avatar asked Jul 28 '15 06:07

Usman Khalid


1 Answers

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.

like image 135
dbc Avatar answered Oct 08 '22 01:10

dbc