Home All Groups Group Topic Archive Search About

Accessing type name from a static method

Author
15 Jan 2005 10:48 AM
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

Author
15 Jan 2005 12:11 PM
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

--
Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
15 Jan 2005 12:34 PM
Vagif Abilov
That's the one I need:

MethodBase.GetCurrentMethod().DeclaringType.Name

Thanks a lot!


Show quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:ubQKbtv%23EHA.1404@TK2MSFTNGP11.phx.gbl...
> 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
>
> --
> Mattias Sjögren [MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.

AddThis Social Bookmark Button