Accessing type name from a static method

V

Vagif Abilov

Hello,

I've come to a problem that I am not able to solve, although something is
telling me there must be a solution.

Let's say I have a class MyType:

public class MyType
{
public MyType() {}

public static void DoSomethingWithTypeName()
{
doSomething("MyType");
}
}

The problem is that I don't want to pass a literal string to doSomething
method - this should not be necessary since this method is executed from
within MyType context, so I should be able to retrieve the type name.
However, since static method does not have access to "this" (for obvious
reason), I can't use .NET Type non-static methods to retrieve class name.
And I can't find any static function that would do that.

I am puzzled because CLR must have such run-time information, but how to get
access to it?

Help is appreciated

Vagif Abilov
vagif @ online.no
Oslo Norway
 
M

Mattias Sjögren

Vagif,
I am puzzled because CLR must have such run-time information, but how to get
access to it?

typeof(MyType).Name

or

MethodBase.GetCurrentMethod().DeclaringType.Name



Mattias
 
V

Vagif Abilov

That's the one I need:

MethodBase.GetCurrentMethod().DeclaringType.Name

Thanks a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top