I have this:
public class PagedResults<T>
{
public int a { get; set; }
public string b { get; set; }
public int c { get; set; }
....
....
public IEnumerable<T> Results { get; set; }
}
When this gets serialised into JSON, I get:
{"a":1, "b":"string", "c":2, ....,"Results":[{...},{....},....] }
but I want:
when <T> = TypeA:
{"a":1, "b":"string", "c":2, ...,"TypeA":[{...},{....},....] }
when <T> = TypeB;
{"a":1, "b":"string", "c":2, ...,"TypeB":[{...},{....},....] }
Tried this with help from here: How to get the name of <T> from generic type and pass it into JsonProperty()?
public class PagedResults<T> : Newtonsoft.Json.Linq.JObject
{
private static string TypeName = (typeof(T)).Name;
public int a { get; set; }
public string b { get; set; }
public int c { get; set; }
....
private IEnumerable<T> _Results { get; set; }
public IEnumerable<T> Results
{
get { return _Results; }
set
{
_Results = value;
this[TypeName] = Newtonsoft.Json.Linq.JToken.FromObject(_Results);
}
}
}
Now, I get the Results array with specific class name, but all the other members (i.e., a, b, c are lost).
I now get when <T> = TypeA:
{"TypeA":[{...},{....},....] }
when <T> = TypeB;
{"TypeB":[{...},{....},....] }
but I want:
when <T> = TypeA:
{"a":1, "b":"string", "c":2, ...,"TypeA":[{...},{....},....] }
when <T> = TypeB;
{"a":1, "b":"string", "c":2, ...,"TypeB":[{...},{....},....] }
Any help is highly appreciated.