0
class ThirdType : SecondType
{
}

class SecondType : FirstType<SecondType>
{
}

class FirstType<T> 
{
    public static void ShowType()
    {       
        Console.WriteLine(typeof(T).Name);
    }
}


ThirdType.ShowType();

In this code, it will print "SecondType"

I want to know if it is possible to print "ThirdType" here?

I need a way when I call the static method ThirdType.ShowType(), it will print out ThirdType. Two ways I can think of is to modify the method to

public static void ShowType<O>() 
{ 
  Console.WriteLine(typeof(O).Name);
}

another way is

public static void ShowType(Type t)
{ 
  Console.WriteLine(t.GetType().Name);
}

I can't think of any other ways

5
  • 2
    There is absolutely no reason it should print ThirdType... it wouldn't make any sense Commented May 31, 2011 at 8:03
  • 2
    why do you need that? Whatever you're trying to do, I'm sure there must be a better way... Commented May 31, 2011 at 8:18
  • 1
    There are no other ways. As you are calling an inherited static method, there is no way for the method to know that you used ThirdType.ShowType() instead of FirstType<SecondType>.ShowType() to call it. Commented May 31, 2011 at 8:20
  • You can't get polymorphism for things that are static. Commented May 31, 2011 at 9:01
  • Thanks all, I think I am actually looking for some sort of GetMethodInvokerBaseType() method from .net. I guess it doesn't exist. Commented May 31, 2011 at 10:23

5 Answers 5

3

No. Why should it print ThirdType? You are printing the type of T which is the generic parameter you passed to FirstType. And this is SecondType: FirstType<SecondType> If you want to print ThirdType you need to change your method to this:

public void ShowType()
{       
    Console.WriteLine(GetType().Name);
}

and call it from an instance of the class:

ThirdType tt = new ThirdType();
tt.ShowType();
Sign up to request clarification or add additional context in comments.

5 Comments

However, GetType is not a static method, so you can't use it here. You have to create an instance of the class to use the GetType method.
@Guffa and @Anton: Please read my answer again. I explicitly state that you need to create an instance. Also please note that ShowType isn't static any more...
@Daniel Hilgarth: It didn't when I wrote the comment.
@Daniel Hilgarth: I can only comment on the answer as it looks at the time, I can not see future changes to it... ;)
This is definitely a solution but my real problem is the method is static, Its not possible for me to change it in my case.
1

You can declare your ThirdType like this:

class ThirdType : FirstType<ThirdType>
{
}

or

class ThirdType : SecondType<ThirdType>
{
}

class SecondType<T> : FirstType<T>
{
}

In this case it would print "ThirdType". But I thinks this would be bad practice, the best solution I can see is described in Daniel's answer

Comments

0

What you can do it to throw the generics out the window, and replace the line

Console.WriteLine(typeof(T).Name);

with

Console.WriteLine(this.GetType().Name);

this.GetType() will return the actual type of the object, in this case ThirdType.

And remove staticfrom the method. At least if you want to make sure this makes any sense.

1 Comment

@Chia - But why do you need it to be a static method? Then the method is not inherited, and always "belong" to ThirdType. Your example does not work either, since you can't call ThirdType.ShowType(). Only FirstType.ShowType(). And when static, the only way to get this method to return what you want is to write the following to the console typeof(ThirdType).Name, or simply "ThirdType". Quite useless of course, but nothing you can do here for a static method.
0

Sure, just add Console.WriteLine("ThirdType") ;-)

Just kidding - no it is not possible with your current object hierarchy because you are printing the type of the generic type argument (<T>) so the only way to have it print ThirdType is if ThirdType inherited from FirstType<ThirdType>.

Comments

0

As You have written in you code

Console.WriteLine(typeof(T).Name);

and T here is "SecondType" which you passed in second class inheriatance .

thants y its printing "SecondType"

Modify you code something like this : may work for you

class ThirdType : SecondType<ThirdType>
{
}

class SecondType<T> : FirstType<T>
{
}

class FirstType<T> 
{
    public static void ShowType<T>()
    {       
        Console.WriteLine(typeof(T).Name);
    }
}

ThirdType.ShowType();

1 Comment

I know, just for example. I really need a way for the caller to know its type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.