mardi 24 juillet 2018

Reflection: Get All properties that implements an interface in a parameter Object

I have a class which has many objects of other classes:

public class Rootobject
{
    public USD USD { get; set; }
    public CAD CAD { get; set; }
    public EUR EUR { get; set; }
    public AED AED { get; set; }
    public AFN AFN { get; set; }
    public ALL ALL { get; set; }
}

Each of these classes implements an interface called ICurrency; the ICurrency Interface has a string property called "symbol" like that:

public class EUR : ICurrency 
{/*code logic*/}

Finally I have a method Which takes the RootObject instance as a parameter;

public object Add22(Rootobject rootobject)
{}

I need to get all the values of the "symbol" property of all the instances passed in the rootobject variable.

I think this can easily be done through reflection by creating a list of ICurrency and adding all the objects in it then looping through it.

Am I right? and if yes; then how to make it? or there is a better approach?





Aucun commentaire:

Enregistrer un commentaire