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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:06:44+00:00 2026-06-02T01:06:44+00:00

This is My Generic Class: public class MyClass<T> { public T MainModel { get;

  • 0

This is My Generic Class:

public class MyClass<T>
{
    public T MainModel { get; set; }

    public Type GetType()
    {
        //here
    }
}

Then I use it this form:

List<MySecondClass> Myinstance = new List<MySecondClass>();

MyClass<IEnumerable<MySecondClass>> mainInstance = new  MyClass<IEnumerable<MySecondClass>> { MainModel = Myinstance  };

So I need the GetType() Method returned typeof(MySecondClass) what is your suggestion?

Update

I must mention that always use the MyClass in just two form: First : I exactly coded in above and Second:

ThirdClass thirdone = new ThirdClass(); 
MyClass<ThirdClass> = new MyClass<ThirdClass>(){ MainModel = thirdone};
  • 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-02T01:06:46+00:00Added an answer on June 2, 2026 at 1:06 am

    If T is List<T>

    // add error checking to taste
    var typeOfEnumerable = typeof(T).GetInterfaces().Where(i => i.IsGenericType)
        .Single(i => i.GetGenericTypeDefinition() == typeof(IEnumerable<>))
        .GetGenericArguments().First();
    

    See it in action.

    If T is IEnumerable<T>

    // add error checking to taste
    var typeOfEnumerable = typeof(T).GetGenericArguments().First();
    

    Important notes

    You really should think about what MyClass is trying to do here. Why does it want to special-case for T being an IEnumerable<X>? Perhaps some constraints should be added on T? It’s not possible to answer these and other important questions without more concrete information, but you really should do so.

    Also, did you name the method GetType by accident or on purpose? It’s really, really, really not a good idea to hide object.GetType like that, so I would suggest you rename that method.

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

Sidebar

Related Questions

How to force set the type to create a generic instance? public class MyClass<T
I have a generic class definition similar to this: public sealed class MyClass<TProperty,TOwner> {
I have a class such as: public MyClass { public myEnumType Status {get;set;} public
I'm trying to make this code generic: public final Object unwrap(Class arg0) { throw
When I try this with a generic class where this.value is T: if (this.value.GetType()
I have a generic class that takes a type T . Within this class
Please explain what this task is about? Create a generic linked list class that
I want to use the DbProvider class to construct a generic DAL component. This
I'm trying to implement a class that stores a generic nullable type: public class
I have a class looking like this: class MyClass { public int Id; public

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.