locked
is a property an object, list and inherit from x class RRS feed

  • Question

  • User1034446946 posted

    Hi

    I am looping through the properties of an object and need to find out is the property is an class or multiple classes (enumerable, collection or list) and inherit a class of x and i am a little stuck, any help would be appriciated.

    Sunday, March 15, 2020 6:52 PM

Answers

  • User711641945 posted

    Hi EnenDaveyBoy,

    Here is a working demo like below:

    1.Model:

    public class Base
    {
        public int Id { get; set; }
    }
    public class Test:Base
    {
        public Test2 Test2 { get; set; }
        public List<Test2> Tests { get; set; }
    }
    public class Test2
    {
        public string Data { get; set; }
    }

    2.Test:

    public void Testmethod()
    {
        var obj = new Test();
        foreach (var property in obj.GetType().GetProperties())
        {
    
            //for complex types
            if (property.PropertyType.IsClass&&!typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
            {
                Console.WriteLine("{0} is a class", property);
            }
            if(typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
            {
                Console.WriteLine("{0} is List class", property);
            }
            //if it is a subclass
            if(typeof(Test).IsSubclassOf(typeof(Base)))
            {
                Console.WriteLine("{0} is inherits from Base", property);
            }              
        }
    }

    Reference:

    https://stackoverflow.com/a/8699063/11398810

    https://stackoverflow.com/a/37844503/11398810

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 16, 2020 5:45 AM

All replies

  • User475983607 posted

    I am looping through the properties of an object and need to find out is the property is an class or multiple classes (enumerable, collection or list) and inherit a class of x and i am a little stuck, any help would be appriciated.

    I think you are looking for an extension method.  

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-implement-and-call-a-custom-extension-method

    Sunday, March 15, 2020 7:41 PM
  • User-474980206 posted

    you use the is operator:

      https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is

    also as core supports c# 8, you can use the new pattern matching:

       https://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching

    Sunday, March 15, 2020 9:49 PM
  • User1034446946 posted

    thanks for the replie, sorry rushed the op. have tried to us is but I am looping through the properties

    foreach(var prop in GetType().GetProperties())
    {
    
    }
        

    do i have to check if its all enumerable and collection and List, or is there one i can check for them all?

    can i use

    if (prop == typeof(MyInheitedClassName))
    {

    }

    Sunday, March 15, 2020 11:06 PM
  • User711641945 posted

    Hi EnenDaveyBoy,

    Here is a working demo like below:

    1.Model:

    public class Base
    {
        public int Id { get; set; }
    }
    public class Test:Base
    {
        public Test2 Test2 { get; set; }
        public List<Test2> Tests { get; set; }
    }
    public class Test2
    {
        public string Data { get; set; }
    }

    2.Test:

    public void Testmethod()
    {
        var obj = new Test();
        foreach (var property in obj.GetType().GetProperties())
        {
    
            //for complex types
            if (property.PropertyType.IsClass&&!typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
            {
                Console.WriteLine("{0} is a class", property);
            }
            if(typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
            {
                Console.WriteLine("{0} is List class", property);
            }
            //if it is a subclass
            if(typeof(Test).IsSubclassOf(typeof(Base)))
            {
                Console.WriteLine("{0} is inherits from Base", property);
            }              
        }
    }

    Reference:

    https://stackoverflow.com/a/8699063/11398810

    https://stackoverflow.com/a/37844503/11398810

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 16, 2020 5:45 AM
  • User1034446946 posted

    thanks for the info, really helped, only bit i changed for my own future reference.

    if(property.PropertyType.IsSubclassOf(typeof(Base)))
    Monday, March 16, 2020 2:13 PM