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

Fir*_*mon 19 c#

我得到了

参数计数不匹配

错误.它出现在该if条款中.我的代码:

private Dictionary<string,string> ObjectToDict(Dictionary<string, string> 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;
}
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为当我将property值添加到字典时它可以正常工作,但是当我测试它是否nullif子句中时却没有.

我发现的所有问题都是在函数调用中输入了不正确数量的参数,但是AFAIK在我的两个调用之间没有什么不同.

the*_*000 43

我很确定这是因为你的对象类型有一个索引属性,你将null传递给GetValue调用的index参数.

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

var properties = obj.GetType().GetProperties()
                    .Where(p => p.GetIndexParameters().Length == 0);
Run Code Online (Sandbox Code Playgroud)