locked
How understand (and cast) Generic Type is List RRS feed

  • Question

  • Hi all,

    I have a generic type coming from another class. So I need to understand that it is entity or entity list and if it is an entity list how could I convert that generic type to list for getting count of items ?

    I created a demo for that scenario

        class Program
        {
            static void Main(string[] args)
            {
                var customer1 = new Customer()
                {
                    Id = 1
                };
    
                var customer2 = new Customer()
                {
                    Id = 2
                };
    
                var list = new List<Customer>();
                list.Add(customer1);
                list.Add(customer2);
                
                //Test.Do(customer1);
                Test.Do(list);
            }
        }
    
        public class Test
        {
            public static void Do<T>(T entity)
            {
    
                if (typeof(T).GetGenericTypeDefinition() == typeof(List<>))
                {
                    var count = ((IList<T>) entity).Count;
                    System.Console.WriteLine($"T is a list and has {count} elements");
                }
                else
                {
                    System.Console.WriteLine($"T is an entity");
                }
            }
        }
    
        public class Customer
        {
            public int Id { get; set; }
        }

    *when "Test.Do(list);" is active (so it's means T is list) this code has an error on that line;

    var count = ((IList<T>) entity).Count;

    The error is : Additional information: Unable to cast object of type 'System.Collections.Generic.List`1[Tester.Console.Customer]' to type 'System.Collections.Generic.IList`1[System.Collections.Generic.List`1[Tester.Console.Customer]]'.

    *when "Test.Do(customer1);" is active (so it's means T is entity) this code has an error on that line;

    if (typeof(T).GetGenericTypeDefinition() == typeof(List<>)) 

    An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

    Additional information: This operation is only valid on generic types.

    So;

    I have two separated problem,

    First problem is dedecting the generic type is list or single entity (if it's list no problem but if it's entity it gives error)

    The other problem is if generic type is list, I can not get the count of that list.

    Saturday, March 18, 2017 9:44 AM

Answers

  • You can do this:

            public static void Do<T>(T entity)
            {
                System.Reflection.PropertyInfo pi = entity.GetType().GetProperty("Count");
                if (pi!=null)
                {
                    var count = pi.GetValue(entity);
                    System.Console.WriteLine($"T is a list and has {count} elements");
                }
                else
                {
                    System.Console.WriteLine($"T is an entity");
                }
            }
    

    Saturday, March 18, 2017 6:09 PM

All replies

  • You can do this:

            public static void Do<T>(T entity)
            {
                System.Reflection.PropertyInfo pi = entity.GetType().GetProperty("Count");
                if (pi!=null)
                {
                    var count = pi.GetValue(entity);
                    System.Console.WriteLine($"T is a list and has {count} elements");
                }
                else
                {
                    System.Console.WriteLine($"T is an entity");
                }
            }
    

    Saturday, March 18, 2017 6:09 PM
  • There are couple of ways to do it. One way is to use is operator to check if it is a List<T> instance like:

    public static void Do<T>(T entity) { if (entity is IList) { var list = entity as IList<T>;

    if(list !=null) System.Console.WriteLine($"T is a list and has {list.Count} elements"); } else { System.Console.WriteLine($"T is an entity"); } }


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by Ehsan Sajjad Saturday, March 18, 2017 7:44 PM
    • Proposed as answer by Ehsan Sajjad Sunday, March 19, 2017 7:51 PM
    Saturday, March 18, 2017 7:41 PM
  • You can do this:

            public static void Do<T>(T entity)
            {
                System.Reflection.PropertyInfo pi = entity.GetType().GetProperty("Count");
                if (pi!=null)
                {
                    var count = pi.GetValue(entity);
                    System.Console.WriteLine($"T is a list and has {count} elements");
                }
                else
                {
                    System.Console.WriteLine($"T is an entity");
                }
            }
    


    It works thank you so much
    Saturday, March 18, 2017 9:22 PM
  • There are couple of ways to do it. One way is to use is operator to check if it is a List<T> instance like:

    public static void Do<T>(T entity) { if (entity is IList) { var list = entity as IList<T>;

    if(list !=null) System.Console.WriteLine($"T is a list and has {list.Count} elements"); } else { System.Console.WriteLine($"T is an entity"); } }


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    Thank you so much Ehsan for your help but unfortunately the list part is not working.
    Saturday, March 18, 2017 9:23 PM