.NET 4.0 equivalent to GetCustomAttribute

I am using a sample project to audit an Entity Framework entity from here

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

Can someone tell me what is equivalent 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: is GetCustomAttribute

not recognized.

+3
reflection c # .net .net-4.0


source to share


1 answer


MemberInfo.GetCustomAttribute<T>()

refers to CustomAttributeExtensions

extension, which contains very thin wrappers for Attribute.GetCustomAttribute()

and Attribute.GetCustomAttributes()

. These wrappers have nullified the returned Attribute

attribute type. See the original source here: https://referencesource.microsoft.com/#mscorlib/system/reflection/CustomAttributeExtensions.cs .



+3


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics