Jump to content
  • Advertisement
Sign in to follow this  
Timberl

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

This topic is 4816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?)

Share this post


Link to post
Share on other sites
Advertisement
Another alternative would be the use of singletons with a static instance of the class. typeof would be a better solution though.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!