System.Attribute.GetCustomAttribute in .NET Core

I am trying to convert the following method (which works fine in the .NET Framework 4.6.2) to .NET Core 1.1.

public static TAttribute GetCustomAttribute<TAttribute>(MemberInfo member) where TAttribute : Attribute
{
    var attr = System.Attribute.GetCustomAttribute(member, typeof(TAttribute));
    if (attr is TAttribute)
       return attr;
    else
       return null;
}

This code gives me an error

The attribute does not contain a definition for GetCustomAttribute.

Any idea what the equivalent of .NET Core should be?

PS I tried the following, but it seems to be an exception. Not sure what the exception is, because it just stops the application. I tried to put the code in a block try catch, but still it just stops working, so I cannot catch an exception.

public static TAttribute GetCustomAttribute<TAttribute>(MemberInfo member) where TAttribute : Attribute
{
   var attr = GetCustomAttribute<TAttribute>(member);
   if (attr is TAttribute)
      return attr;
   else
      return null;
}
+4
source share
3 answers

System.Reflection.Extensions System.Reflection.TypeExtensions, MemberInfo , GetCustomAttribute<T>(), GetCustomAttributes<T>(), GetCustomAttributes() .. . System.Reflection.CustomAttributeExtensions, using:

using System.Reflection;

member.GetCustomAttribute<TAttribute>() , .

+5

GetCustomAttribute:

using System.Reflection;
...

typeof(<class name>).GetTypeInfo()
      .GetProperty(<property name>).GetCustomAttribute<YourAttribute>();
+3

As shown on the page .Net Framework Core official GitHub, use the following

type.GetTypeInfo().GetCustomAttributes()

github link for more info

+1
source

Source: https://habr.com/ru/post/1674172/


All Articles