C#GetDeclaredProperty集值错误方法SetValue的重载没有一个参数

C#GetDeclaredProperty集值错误方法SetValue的重载没有一个参数,c#,dictionary,properties,setvalue,C#,Dictionary,Properties,Setvalue,我的方法中有以下代码(实际上是一个异步静态任务) 试试看 { 字符串responseValues=JSONtoKeyValue(responseBody); Console.WriteLine(“responseValue=“+responseValue”); //var dict=responseValues.Split(“|”).Select(x=>x.Split(“=”).ToDictionary(x=>x[0],x=>x[1]); var dict=responseValues.Spli

我的方法中有以下代码(实际上是一个异步静态任务)

试试看
{
字符串responseValues=JSONtoKeyValue(responseBody);
Console.WriteLine(“responseValue=“+responseValue”);
//var dict=responseValues.Split(“|”).Select(x=>x.Split(“=”).ToDictionary(x=>x[0],x=>x[1]);
var dict=responseValues.Split(“|”)
.选择(x=>x.Split('='))
.Where(x=>x.Length>1&&!String.IsNullOrEmpty(x[0].Trim())
&&!String.IsNullOrEmpty(x[1].Trim())
.ToDictionary(x=>x[0].Trim(),x=>x[1].Trim());
Console.WriteLine(“已创建词典”);
Console.ReadLine();
foreach(dict中的KeyValuePair条目)
{
if(entry.Value==null)
{
dict.Remove(entry.Key);
}
其他的
{
string key=entry.key;
字符串值=entry.value;
if(cnsnt.GetType().GetTypeInfo().GetDeclaredProperty(键)!=null)
{
cnsnt.GetType().GetTypeInfo().GetDeclaredProperty(键).SetValue(值);
}
}
}
}
捕获(System.e例外)
{
控制台写入线(e.Message);
}
我的问题是双重的。首先,我不能为字典处理重复项(如果键已经存在,我只想保留最新的值),但即使我尝试不使用重复项,当我尝试查看是否具有键所具有的属性时,我也会得到错误。方法SetValue的无重载只有一个参数

非常感谢您的帮助。

请查看设定值文档。一个参数确实不存在这种重载


下面是更新字典值的简单方法

如果这是引发错误的行,我们需要知道
cnsnt
是什么。
SetValue
没有只接受一个参数的重载。我不确定你认为问题出在哪里。查看SetValue的文档只有一个参数没有重载。为什么要在这里使用反射?是的,为什么要使用反射和
SetValue
,这让人很困惑。您可能想查阅一本关于如何使用C#中的词典的教程。它比你想象的要简单得多。SetValue方法需要两个参数。要为其设置属性和属性值的类的对象。您需要将SetValue称为
SetValue(cnsnt,value)
非常感谢。。问题是该字符串中有重复的值,我需要以某种方式编辑
var dict=responseValues.Split(“|”)…
,以便在键已经存在时保留最新的值。。也谢谢你的链接。
try
{
    string responseValues = JSONtoKeyValue(responseBody);
    Console.WriteLine("responseValues = " + responseValues);

    //var dict = responseValues.Split('|').Select(x => x.Split('=')).ToDictionary(x => x[0], x => x[1]);
    var dict = responseValues.Split('|')
        .Select(x => x.Split('='))
        .Where(x => x.Length > 1 && !String.IsNullOrEmpty(x[0].Trim())
        && !String.IsNullOrEmpty(x[1].Trim()))
        .ToDictionary(x => x[0].Trim(), x => x[1].Trim());

    Console.WriteLine("Dictionary created.");
    Console.ReadLine();

    foreach (KeyValuePair<string, string> entry in dict)
    {
        if (entry.Value == null)
        {
            dict.Remove(entry.Key);
        }
        else
        {
            string key = entry.Key;
            string value = entry.Value;

            if (cnsnt.GetType().GetTypeInfo().GetDeclaredProperty(key) != null)
            {
                cnsnt.GetType().GetTypeInfo().GetDeclaredProperty(key).SetValue(value);
            }
        }
    }
}
catch (System.Exception e)
{
    Console.WriteLine(e.Message);
}