Is there a way to access an attribute of a C # class?

Is there a way to access an attribute of a C # class?

For example, if I have the following class:

...
[TableName("my_table_name")]
public class MyClass
{
    ...
}

Can I do something like:

MyClass.Attribute.TableName => my_table_name

Thank!

+4
source share
3 answers

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>();
+2
source

You can use reflection to get it. Here is a complete example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    public class TableNameAttribute : Attribute
    {
        public TableNameAttribute(string tableName)
        {
            this.TableName = tableName;
        }
        public string TableName { get; set; }
    }

    [TableName("my_table_name")]
    public class SomePoco
    {
        public string FirstName { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var classInstance = new SomePoco() { FirstName = "Bob" };
            var tableNameAttribute = classInstance.GetType().GetCustomAttributes(true).Where(a => a.GetType() == typeof(TableNameAttribute)).Select(a =>
            {
                return a as TableNameAttribute;
            }).FirstOrDefault();

            Console.WriteLine(tableNameAttribute != null ? tableNameAttribute.TableName : "null");
            Console.ReadKey(true);
        }
    }    
}
+5
source

Here's an extension that will facilitate it by extending the object to give you an attribute helper.

namespace System
{
    public static class ReflectionExtensions
    {
        public static T GetAttribute<T>(this object classInstance) where T : class
        {
            return ReflectionExtensions.GetAttribute<T>(classInstance, true);
        }
        public static T GetAttribute<T>(this object classInstance, bool includeInheritedAttributes) where T : class
        {
            if (classInstance == null)
                return null;

            Type t = classInstance.GetType();
            object attr = t.GetCustomAttributes(includeInheritedAttributes).Where(a => a.GetType() == typeof(T)).FirstOrDefault();
            return attr as T;
        }
    }
}

This will return my previous answer:

class Program
{
    static void Main(string[] args)
    {
        var classInstance = new SomePoco() { FirstName = "Bob" };
        var tableNameAttribute = classInstance.GetAttribute<TableNameAttribute>();

        Console.WriteLine(tableNameAttribute != null ? tableNameAttribute.TableName : "null");
        Console.ReadKey(true);
    }
}   
+2
source

All Articles