4

Within a static class you cannot use the keyword "this" so I can't call this.GetType().GetFullName if I have

public static class My.Library.Class
{
    public static string GetName()
    {
    }
}

Is there anything I can call from within GetName that will return My.Library.Class

3 Answers 3

4

you can get the type of a predetermined class with:

typeof(My.Library.Class).FullName

If you need "the class that declares this method", you'll need to use

MethodBase.GetCurrentMethod().DeclaringType.FullName

However, there's a chance this method will be inlined by the compiler. You can shift this call to the static constructor / initialiser of the class (which will never be inlined - log4net recommends this approach):

namespace My.Library

    public static class Class
    {
        static string className = MethodBase.GetCurrentMethod().DeclaringType.FullName;
        public static string GetName()
        {
             return className;
        }
    }
}

Applying [MethodImpl(MethodImplOptions.NoInlining)] might help, but you should really read up on that if you considering it

HTH - Rob

Sign up to request clarification or add additional context in comments.

5 Comments

you wasted precious seconds typing .GetFullName() ;)
-1 surely that's not quite right, that statement would always give "My.Library.Class"
thanks Adrian - i think i understand the question a bit better now
Yes, much, but Aen was first to answer with MethodBase.GetCurrent().etc, so still -1 :-)
@Adrian I don't really care about the points, I just want to make sure the answer is helpful (for all the other people that come to read this). Cheers.
1
MethodBase.GetCurrentMethod().DeclaringType.FullName

Comments

1
typeof(My.Library.Class).FullName

1 Comment

good point - I forgot the part that turns a Type into a string. +1 for completeness

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.