property.GetValue()中的参数计数不匹配

我得到了

参数计数不匹配

错误。 它出现在if子句中。 我的代码:

 private Dictionary ObjectToDict(Dictionary dict, object obj) { var properties = obj.GetType().GetProperties(); foreach (var property in properties) { if (property.GetValue(obj, null) != null) dict["{{" + property.Name + "}}"] = property.GetValue(obj, null).ToString(); } return dict; } 

这很奇怪,因为当我将property值添加到字典时它可以正常工作,但是当我在if子句中测试它是否为null时则不行。

我发现的所有问题都是在函数调用中输入了不正确数量的参数,但AFAIK在我的两个调用之间没有任何区别。

我很确定这是因为您的对象类型具有索引属性,并且您在GetValue调用上将null传递给index参数。

删除索引属性或从属性变量中筛选出索引属性,例如:

 var properties = obj.GetType().GetProperties() .Where(p => p.GetIndexParameters().Length == 0);