user489041
user489041

Reputation: 28312

Get name of class in C#

I need to get the name of the class that I am currently in. The problem is that I am in a static property. Any idea how I can do this without a reference to this?

Upvotes: 3

Views: 1980

Answers (7)

Andreas
Andreas

Reputation: 1

The solution is fairly obvious just use MakeGenericMethod with T to get the right instance.

But if you want to know the name of T you cannot use the MakeGenericMethod. Another solution is to add a static class with a generic method:

public static class Helper
{
    public static string GetName<T>()
    {
        Type type = typeof(T);
        Type[] genericArguments = type.GetGenericArguments();
        return string.Format("{0}<{1}>", type.Name, string.Join(",", genericArguments.Select(arg => arg.Name)));
    }
}

No change the property ClassName to

public static string ClassName
{
    get
    {
        return Helper.GetName<Generic<T>>();
    }
}

then you will get the correct generic name, for example a call to Generic<DateTime>.ClassName will return "Generic`1<DateTime>".

Upvotes: 0

Michael B
Michael B

Reputation: 7587

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString() Which is mentioned over and over again, can be wrong if the Type is generic. It will not figure out the generic type.

class Generic<T>
{
    public static string ClassName
    {
        [MethodImpl(MethodImplOptions.NoInlining)]
        get 
        {
            return System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString();
        }
    }
}

Also if you don't use the NoInlining directive your code maybe inlined into the callsite which will definitely not yield the results you are after. The following code will print Generic``1[T], rather than the particular instantiation it's being called from. The solution is fairly obvious just use MakeGenericMethod with T to get the right instance.

Upvotes: 0

BlueMonkMN
BlueMonkMN

Reputation: 25601

System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.Name

Upvotes: 3

kennycoder
kennycoder

Reputation: 56

You can use something like:

public static class MyClass
{
  public static string FullName
  {
    get { return typeof(MyClass).FullName; }
  }
}

Upvotes: 1

Michael Arnell
Michael Arnell

Reputation: 1028

The following call should give you the name of the current type...

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name

Upvotes: 1

Chris Shain
Chris Shain

Reputation: 51369

If you are in a static property, you should be able to make the type name a constant.

If you have a static property in a base class that is inherited, and you are trying to determine the type of the child class that you are in, you can't do this with a static property. The reason is that the static property is associated with the base class type, not any base class instance. Use a regular property instead, and use GetType().Name or similar.

Upvotes: 1

George Duckett
George Duckett

Reputation: 32448

If you really want it, although as TomTom points out, you might not need it:

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name

Upvotes: 7

Related Questions