I need deserialize a string with a special encode in a class, like the funcion "JsonConvert.DeserializeObject<>" of NewtonSoft library, I wrote this code:
public void getMembersInfo()
{
Dictionary<String, String> dict = new Dictionary<String, String>();
dict.Add("name", "Name Test");
dict.Add("address", "Addss Test");
Members test = DeserializeObject<Members>(dict);
Console.WriteLine("Var Name: " + test.name);
}
//Really "value" is a string type, but with "Dictionary" is more easy simulate the problem.
public static T DeserializeObject<T>(Dictionary<string, string> value)
{
var type = typeof(T);
var TheClass = (T)Activator.CreateInstance(type);
foreach (var item in value)
{
type.GetProperty(item.key).SetValue(TheClass, item.value, null);
}
return (T)TheClass;
}
public class Members
{
public String name;
public Int age;
public String address;
}
EDIT #1: The problem is that this code not work fine, do not what the problem with my question. It is difficult to explain in another language, so I wrote an example code, in it will should see the problem.