PC Review


Reply
Thread Tools Rate Thread

Accessing type name from a static method

 
 
Vagif Abilov
Guest
Posts: n/a
 
      15th Jan 2005
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


 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      15th Jan 2005
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.
 
Reply With Quote
 
Vagif Abilov
Guest
Posts: n/a
 
      15th Jan 2005
That's the one I need:

MethodBase.GetCurrentMethod().DeclaringType.Name

Thanks a lot!


"Mattias Sjögren" <(E-Mail Removed)> wrote in message
news:ubQKbtv%(E-Mail Removed)...
> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
accessing non-static members from static method Jeff Microsoft C# .NET 1 7th Dec 2008 08:39 AM
Invoking static method by type name Andrus Microsoft C# .NET 3 2nd Sep 2007 07:10 PM
Generic type converter class. Cant call static method of type T. Steve Richter Microsoft C# .NET 9 8th May 2007 09:08 PM
Type info in static method Mattias Sjögren Microsoft C# .NET 4 28th Nov 2006 07:31 AM
Accessing application variable from static method DDE Microsoft C# .NET 4 17th Sep 2003 03:35 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:48 PM.