lundi 25 octobre 2021

How to get only class and IEnumerable from object using reflection in c#?

I need to get only class and IEnumerable from object which can be dynamic(not dynamic keyword in c#). I need to get all properties and their values from class using reflection where property type is class or implements IEnumerable interface. Any suggestions?

 public class Person
{
    public Person()
    {
        NestedPeople = new List<NestedPerson>();
    }
    public string Name { get; set; }
    public List<NestedPerson> NestedPeople { get; set; }
    public List<SomeClass> SomeClasses { get; set; }
    public Base Base { get; set; }
}

public class NestedPerson
{
    public string Name { get; set; }
}

public class Base
{
    public Guid Id { get; set; }
}

public class SomeClass
{
    public Guid Id { get; set; }
}

This is minimal sample code just to explain what I need. I need a method to retrieve properties from the passed object which are either type of class or implements IEnumerable interface. Expected method :

public IEnumerable<object> GetData(object obj)
{
  // need to get properties and values where property is class or implements 
  IEnumerable interface
 
}




Aucun commentaire:

Enregistrer un commentaire