Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7869777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:25:44+00:00 2026-06-03T01:25:44+00:00

I have found myself in a situation where I have a method on a

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:25:46+00:00Added an answer on June 3, 2026 at 1:25 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know what they all do, but have never found myself in a situation
I'm working on a project and have found myself in a situation where I
I have found myself designing a language for fun that is a cross between
I have found myself doing this in my code to 'cache' the work done
I am doing some C programming for school and I have found myself reusing
I am new to fairly new to AS3 and I have found myself needing
I'm currently working on project with Haskell, and have found myself some trouble. I'm
I am developing a fairly large C++ support library, and have found myself moving
I have recently found myself becoming more negative about EF and cannot help wondering
Hopefully a simple question although one I have found impossible to answer myself using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.