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

  • Home
  • SEARCH
  • 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 7930949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:34:39+00:00 2026-06-03T20:34:39+00:00

This is a bit of a lengthy question, so please bear with me. I

  • 0

This is a bit of a lengthy question, so please bear with me.

I need to create a mapping between a set of strings and corresponding generic method calls for each string. However I’ve run into a compile issue, explained lower down.

In my scenario I am using a Dictionary<>, but the issue exists equally for a List<>. For simplicity I’m using a List<> in the example below.

Consider these three classes:

public abstract class MyBase { /* body omitted */  }
public class MyDerived1 : MyBase { /* body omitted */  }
public class MyDerived2 : MyBase { /* body omitted */  }

And a method in some other class:

public class Test
{
    public T GetT<T>() where T : MyBase { /* body omitted */ }
}

In another class, I can declare a List<Func<MyBase>> like this:

public class SomeClass
{
    public void SomeFunc()
    {
        var test = new Test();

        var list1 = new List<Func<MyBase>>
            {
                test.GetT<MyDerived1>,
                test.GetT<MyDerived2>
            };
    }
}

This is all fine and well.

But, what if I want to have a function that return a generic class like this:

public class RetVal<T> where T : MyBase { /* body omitted */ }

public class Test
{
    public RetVal<T> GetRetValT<T>() where T : MyBase
    {
        return null;
    }
}

And I want to create an equivalent List<> using this function. i.e. a List>>?

public class Class1
{
    public void SomeFunc()
    {
        var test = new Test();

        var list2 = new List<Func<RetVal<MyBase>>>
            {
                test.GetRetValT<MyDerived1>, // compile error
                test.GetRetValT<MyDerived2> // compile error
            };
    }
}

I get compile errors of Expected a method with 'RetVal<MyBase> GetRetValT()' signature.

So, is there any way around this, or is there an alternative approach that I can use for creating my string…generic method call mappings?

  • 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-03T20:34:41+00:00Added an answer on June 3, 2026 at 8:34 pm

    C# only allows covariance on interfaces. That means you cannot cast a RetVal<MyDerived1> to a RetVal<MyBase> automatically. If RetVal should be covariant, create an interface for it, like so:

    public interface IRetVal<out T>
    {
    
    }
    public class RetVal<T> : IRetVal<T> where T : MyBase { /* body omitted */ }
    
    public class Test
    {
        public IRetVal<T> GetRetValT<T>() where T : MyBase
        {
            return null;
        }
    }
    

    Then this code will work:

        var list2 = new List<Func<IRetVal<MyBase>>>
            {
                test.GetRetValT<MyDerived1>,
                test.GetRetValT<MyDerived2>
            };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now that it's answered: Don't bother reading this question, it's a bit lengthy and
Sorry as the question is bit lengthy. I have this string Hello how are
Consider this bit of obfuscated code. The intention is to create a new object
Sorry if this is a bit of a starter question but I am pretty
Please, move this question to Code Review -area . It is better suited there
If this question is totally unsuitable, please forgive me. I don't know anything about
First question here, so please forgive me if this seems obvious. I'm having a
I am being thick this morning so please excuse this simple question - I
This is a really stupid question, but I need a little reassurance/assistance. I have
Warning, this is a bit recursive ;) I answered this question: Python:How can i

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.