0

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.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • 1
    Going to need a lot more information for this, like are you using MVC.NET, WebApi, WCF? Something else? If you just want to turn that into JSON, use `DataContractJsonSerializer` or JSON.Net. – crush Nov 18 '15 at 21:29
  • Using WebApi with post to /api/products object posted: {Data : "Name,Price.PriceFormated"} Work well except I always get All the object serialized and would like to be able to selet what parts of the object i get back based on my data in the post –  Nov 18 '15 at 21:40
  • The JSON part is rather trivial once you've got the actual problem solved: generate a type at runtime that only has the specified properties, and the values of the source list for those properties. While an interesting question, a full-blown solution would be too broad. Can you explain what you have tried, and with what part specifically you need help? – CodeCaster Nov 18 '15 at 21:46
  • 1
    Smells like a lot of reflection – Royal Bg Nov 18 '15 at 21:48
  • My first idea was the following using System.Reflection.Emit.TypeBuilder and then create all as string and use AutoMapping to copy the data from object to the new but then have some issues with converting the type¨ –  Nov 18 '15 at 21:51
  • Second i did try find a good way to do with reflection but cant seem to get it right' –  Nov 18 '15 at 21:52
  • 1
    Then please [edit] your question to include your code, the input and the actual result. – CodeCaster Nov 18 '15 at 21:53
  • 1
    Sounds similar to [How to create LINQ Query from string?](http://stackoverflow.com/questions/5139467/how-to-create-linq-query-from-string). It's also possible to use Json.NET to serialize only selected properties using a custom [`ContractResolver`](http://www.newtonsoft.com/json/help/html/contractresolver.htm), or to filter the results afterward. Can you show us a [concrete example](http://stackoverflow.com/help/mcve) of one your API calls? – dbc Nov 18 '15 at 22:47
  • WebApi uses Json.NET. You might consider serializing to a `JToken` hierarchy, filtering the hierarchy using the answer from [How to perform partial object serialization providing “paths” using Newtonsoft JSON.NET](https://stackoverflow.com/questions/30304128), then returning the trimmed-down `JToken` using [Returning raw JSON content from ASP.NET Web API](http://www.bizcoder.com/returning-raw-json-content-from-asp-net-web-api). – dbc Nov 19 '15 at 22:11

0 Answers0