How to get an attribute type declaration from a subtype tree in C #

I go through many times to find out how to get a type declaring an attribute in a subtype hierarchy, but don't find out yet, I hope someone knows how to help me.

Example: we have some things like below:

I have an attribute class to define the table name if needed

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class TableBaseAttribute : Attribute
{
    public string Name;
}

      

then i can use it like

[TableBase] // when not assign Name, will get name of the type which declare attribute, in this case is 'Customer'
class Customer
{

}

class CustomerProxy : Customer
{

}

      

and

[TableBase(Name = "_USER")]
class User
{

}

class UserRBAC : User
{

}

class UserRBACProxy : UserRBAC
{

}

      

So how to solve this

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Table of CustomerProxy is : {0}", GetTableNameFromType(typeof(CustomerProxy)));
        Console.WriteLine("Table of UserProxy is : {0}", GetTableNameFromType(typeof(UserRBACProxy)));
    }

    static string GetTableNameFromType(Type type)
    {
        // what go here to get string of "Customer" for type = CustomerProxy
        //              to get string of "_USER" for type = UserRBACProxy

        TableBaseAttribute tableBaseA = (TableBaseAttribute)typeof(CustomerProxy).GetCustomAttributes(type, true)[0];

        string ret = null;
        if (string.IsNullOrEmpty(tableBaseA.Name))
            ret = ???
        else
            ret = tableBaseA.Name;

        return ret;
    }
}

      

+3


source to share


2 answers


I'm not sure if I understand you correctly. You need the name of the type on which the attribute is declared, right? If so, this should work:



    static string GetTableNameFromType(Type type)
    {
        // what go here to get string of "Customer" for type = CustomerProxy
        //              to get string of "_USER" for type = UserRBACProxy

        TableBaseAttribute tableBaseA = (TableBaseAttribute)type.GetCustomAttributes(typeof(TableBaseAttribute), true)[0];

        string ret = null;
        if (string.IsNullOrEmpty(tableBaseA.Name))
        {
            do
            {
                var attr = type.GetCustomAttributes(typeof(TableBaseAttribute), false);
                if (attr.Length > 0)
                {
                    return type.Name;
                }
                else
                {
                    type = type.BaseType;
                }
            } while (type != typeof(object));
        }
        else
        {
            ret = tableBaseA.Name;
        }

        return ret;
    }

      

0


source


You can use the Name property of this type if no attribute override is specified, this will only give you the class name

ret = String.IsNullOrEmpty(tableBaseA.Name) ? type.Name : tableBaseA.Name;

      




I just realized that the above would not work in the context of pulling the TableBase

decorated type's class name , but instead take the current type's class name. In this case, you will need to work on the inheritance tree to find this base class

var baseType = type.BaseType;
while (baseType != null)
{
    var attrs = baseType.GetCustomAttributes(typeof(TableBaseAttribute), false);
    if (attrs.Length > 0) 
    {
        ret = baseType.Name;
        break;
    }
}

      

0


source







All Articles