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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:41:19+00:00 2026-05-24T01:41:19+00:00

I have written the following code where I am trying to determine whether a

  • 0

I have written the following code where I am trying to determine whether a generic classes type inherits from a base class. I think this is easier to explain what I am doing in code. Could anybody please provide some insight into how to get around this issue.

public class MyGeneric<T>
{
}

public class MyBaseClass
{
}

public class MyClass1 : MyBaseClass
{
}

static void Main(string[] args)
{
    MyGeneric<MyClass1> myList = new MyGeneric<MyClass1>();

    if(myList.GetType() == typeof(MyGeneric<>))
    {
        // Not equal
    }

    // This is the test I would like to pass!
    if(myList.GetType() == typeof(MyGeneric<MyBaseClass>))
    {
        // Not equal
    }

    if(myList.GetType() == typeof(MyGeneric<MyClass1>))
    {
        // Equal
    }
}
  • 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-24T01:41:19+00:00Added an answer on May 24, 2026 at 1:41 am

    You need to use Type.GetGenericArguments to get an array of the generic arguments, and then check if they are part of the same hierarchy.

    MyGeneric<MyClass1> myList = new MyGeneric<MyClass1>();
    
    if(myList.GetType() == typeof(MyGeneric<>))
    {
        // Not equal
    }
    
    // WARNING: DO NOT USE THIS CODE AS-IS!
    //   - There are no error checks at all
    //   - It should be checking that myList.GetType() is a constructed generic type
    //   - It should be checking that the generic type definitions are the same
    //     (does not because in this specific example they will be)
    //   - The IsAssignableFrom check might not fit your requirements 100%
    var args = myList.GetType().GetGenericArguments();
    if (typeof(MyBaseClass).IsAssignableFrom(args.Single()))
    {
        // This test should succeed
    }
    

    See also How to: Examine and Instantiate Generic Types with Reflection at MSDN.

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

Sidebar

Related Questions

I have written the following simple test in trying to learn Castle Windsor's Fluent
I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class
I have written the following code as, Dim report As New ReportDocument report.PrintOptions.PrinterName =
I'm playing around with Ruby and I have written the following code: module IdAndNameRedefine
I have written following code to make a call also have set permissions in
I have written this code and what I am trying to do here is
I have written something that uses the following includes: #include <math.h> #include <time.h> #include
I have written a KornShell (ksh) script that sets an array the following way:
(All of the following is to be written in Java) I have to build
I have written an AIR Application that downloads videos and documents from a server.

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.