-3

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.

Manux22
  • 323
  • 1
  • 5
  • 16
  • 2
    What's your specific question? Can you give more detail about the problem you're having? What isn't going according to plan? – rutter Jun 10 '15 at 23:40
  • Sorry, but not how to explain it, I wrote that code and I need it to work, if you read the code you will understand what I need, i have a dictionary with two values, name of variable and value of variable, i need create a Members class and set all variables to value of dictionary, and finally return this new class. (The class is "T"). – Manux22 Jun 11 '15 at 00:06
  • 3
    That's not a question. – JK. Jun 11 '15 at 00:06
  • 1
    Does the code work? If not, what is it doing wrong? If yes, what's the problem? – vesan Jun 11 '15 at 00:18
  • you test my code? The problem is that my code not work fine, but "Enigmativiti" solved my problem, the error is with `GetProperty`, should be `GetField`. – Manux22 Jun 11 '15 at 14:22

1 Answers1

0

It would be extremely helpful if the code you posted in your question would actually compile. It really doesn't seem like you've taken the time to even try your code before you posted. With a little help from the compiler I worked out that you code should be like this:

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.GetField(item.Key).SetValue(TheClass, item.Value);
    }
    return (T)TheClass;
}

public class Members
{
    public String name;
    public int age;
    public String address;
}

The only thing that I think I can say that was an actual logic error was that you were using .GetProperty(...) when you should have been using .GetField(...).

Otherwise it would suggest dropping the (T) on the line return (T)TheClass; and also you should put a where T : new constraint on the DeserializeObject<T> method and then simply call var TheClass = new T();.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • You say I not tried the code? i wrote the code completely. Obviously this not work, it is the problem, if it worked properly, I would not be asking here. I do not ask for help until i have tried everything. Thanks for you help, i go to test it. Sorry for my english, I do what I can. – Manux22 Jun 11 '15 at 03:50
  • Thanks you, now this code work, the problem was the function `GetProperty` – Manux22 Jun 11 '15 at 14:21