vendredi 14 octobre 2022

C# Reflecting into a subclass

I'm trying to reflect into a class instance within a class. I can reflect into the class (Jig) but when I get the config property and try to reflect into that it gives me system. properties and not the properties within that Config class. Is it possible to reflect into the config class in the current manner?

Class structure:

public class Jig : IJig
{
    public string ConfigQuery { get; }
    public string ResultsQuery { get; }

    public Config Config { get; set; } = new Config();

    public static class Results { }
}


public class Config
{
    public testClass testClassProp { get; set; } = new testClass();

    public string prop {get; set;}
}

public class testClass
{
    public string Testprop { get; set; }
    public int avs {get; set;}
}

Reflection:

    public async Task Initialise()
    {
            await SetConfigs(Jig.Config.GetType());
    }

    
    private async Task SetConfigs(Type configType)
    {
        List<PropertyInfo> propertyInfos = configType.GetProperties().ToList();

        bool IsUserDefinedClass(PropertyInfo x) { return x.PropertyType.Assembly.FullName == configType.Assembly.FullName && x.PropertyType.IsClass; }
        List<PropertyInfo> subClass = propertyInfos.FindAll(x => IsUserDefinedClass(x)).ToList();
        propertyInfos.RemoveAll(x => IsUserDefinedClass(x));

        foreach (PropertyInfo x in subClass)
        {
            await SetConfigs(x.GetType());
        }
    }




Aucun commentaire:

Enregistrer un commentaire