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 1050453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:46:23+00:00 2026-05-16T16:46:23+00:00

I have the following scenario: class Foo { } class Foo<T> : Foo {

  • 0

I have the following scenario:

class Foo { }

class Foo<T> : Foo { }

And then two methods

void DoStuff(Foo foo) 
{
     DoStuffImpl(foo);
}

void DoStuffImpl(Foo foo) 
{ 
     Console.WriteLine("A");
}    
void DoStuffImpl<T>(Foo<T> foo) 
{ 
     Console.WriteLine("B");
} 

void Main() 
{
     DoStuff(new Foo<int>()); // prints A
}

(note, the code was written in the browser, but describes the situation I’m facing)

How can I get it to call the generic method, and print B?

Can this be done at all without reflection? I have some ideas on how it could be done with reflection, but I’m looking for a cleaner solution if one exists.

Note: I can’t make DoStuff generic because this will be used with WCF and open generic types are not allowed.

  • 1 1 Answer
  • 1 View
  • 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-05-16T16:46:23+00:00Added an answer on May 16, 2026 at 4:46 pm

    (I assume you already understand why this is happening. If not, read my overload resolution article and let me know if it’s still unclear.)

    If you’re using C# 4 you could use dynamic typing:

    void DoStuff(Foo foo) 
    {
        dynamic d = foo;
        DoStuffImpl(d);
    }
    

    Note how this doesn’t just have a dynamic parameter – the idea is that by restricting foo to be of type Foo or a subclass, we’ll always have a valid DoStuffImpl to call… it’s just that the best method will be determined at execution time, not compile time.

    If you’re stuck in pre-C# 4, you could potentially do it with double dispatch:

    class Foo
    {
        public virtual void CallStuffImpl(FooImplType x)
        {
            x.DoStuffImpl(this);
        }
    }
    
    class Foo<T> : Foo
    {
        public override void CallStuffImpl(FooImplType x)
        {
            // Looks like it's redundant, but isn't! "this" is
            // known to be Foo<T> rather than Foo
            x.DoStuffImpl(this);
        }
    }
    

    Then:

    void DoStuff(Foo foo) 
    {
        foo.CallStuffImpl(this); // Let it dispatch appropriately
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following scenario, class foo { ... private: char *_test; }; void
I have the following scenario: class A { string Foo; } Class B {
I have the following scenario public class Foo { public Bar FooBar { get;
If i have the following scenario: public class Foo extends Baz{ private String banana
Have a look at following scenario: public class ParentClass { private Integer testVar =
I have the following scenario :- template <typename T> class Base { public: virtual
I have the following scenario: public class A { } public class B extends
I have this following scenario namespace A { //... class A { public: static
I have the following scenario: class my_base { ... } class my_derived : public
I have the following scenario: class Addition{ public Addition(int a){ a=5; } public static

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.