C# Is it possible to get the type of a class in its static method?
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?)
Another alternative would be the use of singletons with a static instance of the class. typeof would be a better solution though.
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!
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: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.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement