lundi 10 février 2020

Identifying TypeOf value of Property Attributes as existing Class name

I am trying to iterate through Classes who have properties not linked to other Classes (i.e. public SomeOtherClass code { get; set; }

In my attempt and thanks to other people's advice, I came up with the following, which iterates through the classes of a specific namespace (i.e. Library = "ConsoleApp1.Library" etc) as:

        var classes = AppDomain.CurrentDomain.GetAssemblies ()
                                .SelectMany (t => t.GetTypes ())
                                .Where (t =>
                                            t.IsClass &&
                                            t.Namespace == Library &&
                                            !t.IsNested)
                                .ToList ();

and getting properties as

        var properties = classes.SelectMany (x => x.GetProperties (BindingFlags.Public | BindingFlags.Instance)
                                .Select (y => y.PropertyType));

while I get what I need from a foreach loop:

        foreach ( var method in classes.Where 
                (x => !properties.Contains(x) && !properties.Contains (x))
                .Select (x => x.Name) )
        {
        // some work here
        }

However, some cases slipped through my selection; cases where the class has the below Properties as Array, ICollection, List and Dictionary:

public class TopObject
{
    [JsonProperty("ex")]
    public OtherResults ex { get; set; }

    [JsonProperty ("Results")]
    public OtherResults Results { get; set; }

    private readonly OtherResults[,] Codes1 = new OtherResults[9, 9];
    public ICollection<OtherResults> Codes2 { get; set; }
    public List<OtherResults> Codes3 { get; set; }
    public Dictionary<int, OtherResults> Map { get { return _map; } }

    public TopObject()
    { Results = new OtherResults (); }
}

public class OtherResults
{
    [JsonProperty ("Jcodes")]
    public string Jcodes { get; set; }

    public OtherResults()
    { }
}

I need help in editing the var method, to include (additionally) the cases where a property has Dictionary Value type any of the classes, or is Array of type of any of the classes, or is ICollection or List who accept any knows classes.

Kindly someone can help me with this thing? Thank you very much





Aucun commentaire:

Enregistrer un commentaire