Hunt
Hunt

Reputation: 8425

dynamically assign values to IList<T> using generics

I am trying to set the property of the class member dynamically using reflection

Here is my class and I am trying to set the list dynamically i.e. ResponseBody

public class Response<T>  where T : RealmObject
{
    public string errorMessage { get; set; }
    public string status { get; set; }

    public string totalPages {get;set;}


    public IList<T> ResponseBody { get; set; }
}

following is the response from server that I am trying to map it in ResponseBody

{
  "errorMessage": "",
  "status": "Ok",
  "totalPages": 1,
  "contactsList": [
    {
      "firstName": "Ronald",
      "lastName": "Brownn"
    },
    {

      "firstName": "Elvis",
      "lastName": "Presley"
    }
  ]
}

in order to change map the contactsList to ResponseBody inside the JsonConverter I have writting following code

Following code will create Response<Contact> dynamically, consider entityType as Contact

var responseObj = typeof(Response<>).MakeGenericType(new[] { entityType });
var response = Activator.CreateInstance(responseObj);

Following code will dynamically create object IList<Contact>

var responseDataList = typeof(List<>).MakeGenericType(new[] { entityType });
var responseList = Activator.CreateInstance(responseDataList);
var responseEntityList =  jArray.ToObject(responseDataList);

now I want to assign IList<Contact> to Response<Contact>'s ResponseBody member and I really don't know how to do that

Upvotes: 0

Views: 230

Answers (2)

Dan Rayson
Dan Rayson

Reputation: 1417

I don't think that reflection is needed here - Deserialisation should be able to handle lists just fine.

I also found why you're probably not seeing the results you expected; You have contactsList in your JSON, but ResponseBody in your C# POCO. That'll be why!

Here's a working version:

Response Class

public class Response<T> where T : class
{
    public string errorMessage { get; set; }
    public string status { get; set; }

    public string totalPages { get; set; }


    public IList<T> ResponseBody { get; set; }
}

JSON used

{
    "errorMessage": "",
    "status": "Ok",
    "totalPages": 1,
    "ResponseBody": [  <-- notice I changed this
        {
            "firstName": "Ronald",
            "lastName": "Brownn"
        },
        {

            "firstName": "Elvis",
            "lastName": "Presley"
        }
    ]
}

Actual code:

class Program
{
    //Here I'm loading in your json for testing.
    static string input = File.ReadAllText("Input.json");

    static void Main(string[] args)
    {
        Type genericResponseType = typeof(Response<>).MakeGenericType(new[] { typeof(Person) });
        var result = JsonConvert.DeserializeObject(input, genericResponseType);

        Console.WriteLine(result);

        Console.ReadKey();
    }
}

Person class:

public class Person
{
    public string firstName { get; set; }
    public string lastName { get; set; }
}

Upvotes: 2

Rup
Rup

Reputation: 34408

You need to get a PropertyInfo object from the class definition:

var responseBodyProperty = responseObj.getProperty("ResponseBody");

then you can use this to set the value on your object:

responseBodyProperty.setValue(response, responseList);

I don't think it matters at this point that there are generics involved. You've already covered that in your type creation.

Upvotes: 2

Related Questions