C# Is it possible to get the type of a class in its static method?

Started by
16 comments, last by Timberl 18 years, 7 months ago
Is it possible to get the type of a class from inside its static method in C#? (for that matter is it possible in Java?)
Advertisement
typeof(ClassName) should be what you want.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Another alternative would be the use of singletons with a static instance of the class. typeof would be a better solution though.
Rob Loach [Website] [Projects] [Contact]
Sorry, I should have been more specific, I want the method to be inherited and in a subclass the method should get the subclass type -- is a bit easier without this constraint!

Quote:Original post by antareus
typeof(ClassName) should be what you want.


I'm sorry Rob, I don't know what you mean -- unless you mean make an instance of the class to inspect, which kind of defeats the point of using a static method - though you're saying use the singleton so the overhead of the object to type only creates the object once?
Quote:Sorry, I should have been more specific, I want the method to be inherited and in a subclass the method should get the subclass type


Static methods cant be inherited.

But to illustrate the example without inheritance:

public class MyClass
{
public static Type GetClassType()
{
return typeof(MyClass);
}
}

That's how you use the typeof-operator.
Quote:Static methods cant be inherited.


Of course you can inherit static methods.

you can't override them I'll grant you - you can certainly inherit them.

inheritance is the crucial part of the problem here -- because you don't *know* the class of your object since it might be a subclass
Quote:Original post by Timberl
inheritance is the crucial part of the problem here -- because you don't *know* the class of your object since it might be a subclass

But static methods are always invoked on a type, not an object. Since static methods cannot be virtual, there is never any question what type the method was invoked on.

In short, your question makes no sense.

--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Quote:Original post by Timberl
Is it possible to get the type of a class from inside its static method in C#?

When you have an instance of the type to inspect, you use .GetType(), inherited from object. If you don't (for instance, in the case of a static method), the typeof() operator is the correct solution.

If you want the actual string corresponding to that type (e.g. "System.Int32" for typeof(int)), call ToString() on the resulting System.Type.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
As Arild Fines said, it makes absolutely no sense.

A static method is has only one "instance" in memory; every time you call it, it's that same code that is called. It receives no this pointer. How can you expect it to return different results depending on who calls it?

Tell us what you intend to do with it and we might provide you with a better solution.
No inheritance is taking place with static methods. The base class's implementation may be obscured by a subclass implementation, but that isn't the same thing as inheritance. If you're interested in knowing the type of the derived class, add a Type argument to the static method's argument list. Classes (be they base or derived) can simply pass in this.GetType() (or obj.GetType()) for that argument.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

This topic is closed to new replies.

Advertisement