mardi 10 avril 2018

How to get whole types that is used in a class with Roslyn or Reflection? (all nested/inner types recursively)

Please consider this sample:

public class Person
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public float Age { get; set; }
    public List<Address> Addresses { get; set; }
    public IEnumerable<Job> Jobs { get; set; }
    public IInterface MyInterface { get; set; }
}

public class Address
{
    public string City { get; set; }
    public string[] Phones { get; set; }
    public MyEnum E1  { get; set; }
}

public class Job
{
    public Dictionary<decimal, Address> A1 { get; set; }
    public Collection<DateTime> Date { get; set; }
    public Tuple<double, BigInteger> A2 { get; set; }
}

public enum MyEnum
{
   En1,
   En2
}

As you see, I want to get all inner classes/structs/types of Person so the result is :

Person.GetInnerTypes():

Guid
float
string
IInterface
Address
string[]
MyEnum
Job
List<Address>
IEnumerable<Job>
Dictionary<decimal, Address>
decimal
Collection<DateTime>
DateTime
Tuple<double, BigInteger>
double
BigInteger

The types are collected from everywhere (properties, arguments,...)

Is it possible to find whole types (recursively) by Roslyn or Reflection? Does anyone have an idea?

EDIT:

Why do I need this?

The problem comes from creating a code generator, if you see the Bogus library you should define rules for every type at first then create the main rule for Person class so I need to know all types for a class to create a code generator for generating test data! (generate a working class)





Aucun commentaire:

Enregistrer un commentaire