Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在泛型方法c中反序列化字符串_C#_Deserialization_Generic Method - Fatal编程技术网

C# 在泛型方法c中反序列化字符串

C# 在泛型方法c中反序列化字符串,c#,deserialization,generic-method,C#,Deserialization,Generic Method,我需要在类中使用特殊编码反序列化字符串,如库的函数JsonConvert.DeserializeObject,我编写了以下代码: public void getMembersInfo() { Dictionary<String, String> dict = new Dictionary<String, String>(); dict.Add("name", "Name Test"); dict.Add("address", "Addss Test

我需要在类中使用特殊编码反序列化字符串,如库的函数JsonConvert.DeserializeObject,我编写了以下代码:

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;
}
编辑1:
问题是这个代码工作不好,不知道我的问题出在哪里。这很难用另一种语言解释,所以我写了一个示例代码,用它可以看出问题所在。

如果您在问题中发布的代码能够真正编译,那将非常有帮助。在发布之前,您甚至没有花时间尝试代码。在编译器的帮助下,我发现您的代码应该是这样的:

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;
}
我想我唯一能说的是一个实际的逻辑错误是您使用了.GetProperty。。。当您应该使用.GetField时


否则,建议将T放在返回T类的行上;您还应该在反序列化对象方法上设置where T:new约束,然后简单地调用var TheClass=new T

你的具体问题是什么?你能详细介绍一下你遇到的问题吗?什么不符合计划?抱歉,但不是如何解释,我写了代码,我需要它工作,如果你阅读代码,你就会明白我需要什么,我有一个字典,有两个值,变量名和变量值,我需要创建一个成员类,并将所有变量设置为字典值,最后返回这个新类。这个类是T。这不是问题。代码有效吗?如果没有,它做错了什么?如果是,有什么问题?你测试我的代码?问题是我的代码不是很好,但Enigmativiti解决了我的问题,错误是GetProperty,应该是GetField。你说我没有尝试代码?我完整地编写了代码。显然这不起作用,这是个问题,如果它能正常工作,我就不会在这里问了。在我尝试过所有方法之前,我不会寻求帮助。谢谢你的帮助,我去测试一下。对不起我的英语,我做我能做的。谢谢你,现在这段代码工作了,问题是函数GetProperty