You can use Attribute.GetCustomAttributefor this:
var tableNameAttribute = (TableNameAttribute)Attribute.GetCustomAttribute(
typeof(MyClass), typeof(TableNameAttribute), true);
However, this is too much for my taste, and you can make your life much easier with the following small extension method:
public static class AttributeUtils
{
public static TAttribute GetAttribute<TAttribute>(this Type type, bool inherit = true) where TAttribute : Attribute
{
return (TAttribute)Attribute.GetCustomAttribute(type, typeof(TAttribute), inherit);
}
}
so you can just use
var tableNameAttribute = typeof(MyClass).GetAttribute<TableNameAttribute>();
source
share