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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:19:21+00:00 2026-06-01T08:19:21+00:00

Say that you’ve got IList or List as a property. How do you work

  • 0

Say that you’ve got IList or List as a property. How do you work out that it is a List, or an IList? Can this be done without relying on trial and error?

The name of the type tends to be something like List`1. Is it reasonable to consider a string hack?

class Program {

    public class Class1 {
        public int a { get; set; }

        public IList<int> list { get; set; }


        public List<int> concreteList { get; set; }
    }

    static void Main(string[] args)
    {
        Test1();
        Test2();
    }

    private static void Test1()
    {
        var t = typeof (Class1);
        var p = t.GetProperty("list");

        if (p.PropertyType.IsInterface && p.PropertyType.IsGenericType)
        {
            var ps = p.PropertyType.GetGenericArguments();
            var underlying = p.PropertyType.GetInterface("IList");

            var b = underlying == typeof (IList<>);

        }
    }

    private static void Test2() {
        var t = typeof(Class1);
        var p = t.GetProperty("concreteList");

        if (!p.PropertyType.IsInterface && p.PropertyType.IsGenericType) {
            var ps = p.PropertyType.GetGenericArguments();

            var underlying3 = p.PropertyType.GetGenericTypeDefinition();

            var b = underlying3 == typeof (List<>);
        }
    }
}
  • 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-01T08:19:22+00:00Added an answer on June 1, 2026 at 8:19 am

    If you can get the property value then testing for its type can be quite straight-forward (see Guffa’s answer). However, if you want to find it out without invoking the property then your code is almost there – for example,

    var t = p.PropertyType;
    if (t.IsGenericType && !t.IsGenericTypeDefinition && !t.IsInterface && !t.IsValueType) 
    {
       // we are dealing with closed generic classes 
       var typeToTest = typeof (List<>);
       var tToCheck = t.GetGenericTypeDefinition();
       while (tToCheck != typeof(object)) 
       {
          if (tToCheck == typeToTest) 
          {
             // the given type is indeed derived from List<T>
             break; 
          }
          tToCheck = toCheck.BaseType;
       }
    }
    

    IsGenericType indicates that the type is generic – can be open (List<T>) or closed (List<int>). IsGenericTypeDefinition indicates if the generic type is an open type or not. GetGenericTypeDefinition on closed/open generic type will return the generic definition (i.e. the open generic type).

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

Sidebar

Related Questions

Say that I've got a web application that can store Persons in a database.
Say that i have a generic dictionary with data like this (I hope the
Say that I have two html nodes like this: <div class='node a'>one</div> <input class='node
We say that java follows a single inheritance model i.e. a Java class can
Say that I have made some software (which has both library form and can
Say that I have an application where user can upload his avatar, and then
Say that you have an application where different kind of users can sign: Firms,
Say that I have two models- Users and Accounts. Each account can have at
Say that I have two resources, Project and Task. A Project can have many
Say that I have this code: rentalEaseDataSet.tblCheckbookEntry.Rows.Add(newRow); I want to get the identity of

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.