mercredi 18 novembre 2015

Select only a specified set of properties from a list of objects

Given a List<Product> of the following definition:

public class Product
{
    public String Name;
    Public String Description;
    public Price price;
}

public class Price 
{
    float Price;
    String PriceFormated;
}

Now I need to be able to fulfill an Ajax request where I'm requested only the specified property names in a string array, such as { "Name", "Price.PriceFormated" }. I think I need to use reflection for this.

This would be the resulting response, only showing the requested properties:

[
    {
        "Name": "Something"
        "Price" : {
            "PriceFormated": "100 USD"
        }
    },{
        "Name": "Something1"
        "Price" : {
            "PriceFormated": "101 USD"
        }
    }
]

It need to work on any class, not just the ones shown.





Aucun commentaire:

Enregistrer un commentaire