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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:58:36+00:00 2026-05-26T13:58:36+00:00

Let’s say I have these classes: public class BaseMapClass<T1, T2> { protected T1 Thing1;

  • 0

Let’s say I have these classes:

public class BaseMapClass<T1, T2>
{
   protected T1 Thing1;
   protected T2 Thing2;
}

public class InstanceMapClass1 : BaseMapClass<Type1, Type2>
{
      public InstanceMapClass(Type1 x, Type2 y)
      {
         Thing1 = x;
         Thing2 = y
      }
}

public class InstanceMapClass2 : BaseMapClass<Type3, Type4>
{
      public InstanceMapClass(Type3 x, Type4 y)
      {
         Thing1 = x;
         Thing2 = y
      }
}

Now I have a third class, I’d ideally want to have two generic IEnumerables that take their type based on the generics of the type passed in BaseMapClass

public class Three<T> where T : BaseMapClass<dynamic, dynamic>
{
   IEnumerable<??> TypeXThings; // should be of the first dynamic type
   IEnumerable<??> TypeYThings; // should be of the second dynamic type
}

so if I created a new class

Three<InstanceMapClass1> three = new Three<InstanceMapClass1>();
Type1 t1 = three.TypeXThings.FirstOrDefault();

or

Three<InstanceMapClass2> another = new Three<InstanceMapClass2>();
Type3 t3 = another.TypeXThings.FirstOrDefault();

Using dynamics and generics is it possible to set the type of these IEnumerables to the generic types of the BaseMapClass at runtime? What I really want is to be able to pass in this one mapping class and then be able to create collections based on their generic parameters. I feel like I’m going about this the wrong way, but the only other way I can think of doing it is

public class Three<T1, T2, T3>
{
   IEnumerable<T2> Type1Things;
   IEnumerable<T3> Type2Things:
}

Thanks! Also, I realize this may make no sense so please let me know where clarification is needed.

  • 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-05-26T13:58:37+00:00Added an answer on May 26, 2026 at 1:58 pm

    From your question:

    public class Three<T> where T : BaseMapClass<dynamic, dynamic>
    

    dynamic isn’t used this way, and has nothing to do with generics. It is basically an object that supports any method/property/field, and uses runtime compilation to pull up each method/property/field access.

    BaseMapClass<int, string> myVar = new BaseMapClass<int, string>();
    
    object myVarAsObject = myVar;
    Console.WriteLine(myVarAsObject.Thing1); // Won't compile
    
    dynamic myVarAsDynamic = myVar;
    Console.WriteLine(myVarAsDynamic.Thing1); // Will compile to a runnable program
    

    This isn’t a magic bullet, and will still throw an exception at runtime – RuntimeBinderException – because Thing1/Thing2 are protected.

    This is the blessing and the curse of dynamic‘s use of runtime compilation 🙂

    Solution (to get your code to compile)

    Your third example makes the most sense. Here it is combined with your existing code:

    public class Three<T1, T2, T3> where T1 : BaseMapClass<T2, T3>
    {
       IEnumerable<T2> Type1Things;
       IEnumerable<T3> Type2Things:
    }
    

    Though you’ll still inherit:

    protected T2 Thing1; // Note: T1 of base type becomes T2 of the outer type
    protected T3 Thing2; // Note: T2 of base type becomes T3 of the outer type
    

    If this isn’t desirable, don’t inherit from BaseMapClass, or change how it is defined.

    Suggestions on your final design

    If this isn’t helping you solve your original problem, you might want to post your code on the Code Review site and see if you can get some feedback on your design.

    My initial thoughts are that you should investigate if it would make sense to use interfaces in your design, rather than inheriting from concrete types.

    Also see if there are existing types in the .Net framework that suit your purposes instead of inventing new ones (e.g. Tuple<T1, T2> or maybe Dictionary<T1, T2>).

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

Sidebar

Related Questions

Let's say I have the following entity: public class Store { public List<Product> Products
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's imagine I have a Java class of the type: public class MyClass {
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's say I'm building a data access layer for an application. Typically I have
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,

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.