typeof this in virtual methods called from derived class

I have found myself in a situation where I have a method on a base class which makes a call to a generic method passing itself as a parameter (via this). In the generic method the type is always the base type, even when I call the method from a derived class.

I need the generic method to know that it is of the derived type and not the base type.

I am not able to change the generic method since it is from a third party app (http://razorengine.codeplex.com/ - Razor.Parse method).

I have found that if I define the method as a generic extension method restricted to types inheriting from my base class that calling this then works but it seems messy to use extension methods for this purpose (though if this is correct usage I'd be happy to hear that). What I am wondering if there is some trick I can use that will convert "this" to the type of the derived object before it gets called (maybe using generics still).

I have included a simplified program below that should be self contained (I'm running it in LINQPad for personal ease). Essentially I want a method defined on A that when called on an instance of B will call Test.WhatType and output "B".

I hope this makes sense.

void Main()
{
    var bar = new B();
    bar.myGetType();
    bar.ExtGetType();
}

class A
{
    public virtual void myGetType()
    {
        Test.WhatType(this);
        this.ExtGetType();
    }
}

class B : A {}

static class ext
{
    public static void ExtGetType<T>(this T obj) where T : A
    {
        Test.WhatType(obj);
    }
}

class Test
{
    public static void WhatType<T>(T obj)
    {
        Console.WriteLine(typeof(T).Name);
    }
}

Edit: Changed method name to A.myGetType() since I hadn't even noticed I was overloading accidentally.

N.B. Running this code outputs:

A
A
B

Answers


If I understand correctly, your problem is that A.GetType() is binding to WhatType<A> as that is all it knows about.

So you have to let class A know about the derived classes.

You can do this with self-referencing generic types. Something like:

class A<T> where T : A<T>
{
    public virtual void GetType()
    {
        Test.WhatType<T>( (T) this );
    }
}

class B : A<B> { }

then:

new B().GetType();

will bind to WhatType<B> and so will output B


You need to use obj.GetType(). T is bound at compile-time, not a run time and thus typeof(T) returns the compile-time type of T.


This method returns the type of the static type (T). It should be implemented as

public static void WhatType<T>(T obj) 
{ 
    Console.WriteLine(obj.GetType().Name); 
} 

Edit: I just realized what you want is to manipulate A in such a way that the T of the generic method changes. You can either do this via reflection or by adding a switch statement based on the subtype of A and explicitly cast this to B (or whatever class). Of course none of this solutions is good because A would take dependency on its subclasses and reflection plainly sux. Another option is to override the GetType() method in each subclass and thus move the responsibility to the subclasses.


Need Your Help

ShowMessage() doesn't work for iOS 7 simulator in Delphi xe5

ios delphi firemonkey delphi-xe5

After installing update 1 and the iOS 7 styles for delphi xe5, the function, "ShowMessage('some text to show');" does not work properly. The function seems to execute fine in the debugger but withi...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.