Parameter counter mismatch in property.GetValue ()

I get

mismatch in the number of parameters

error. This happens in the if clause. My code is:

 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; } 

This is strange because it works fine when I add the property value to the dictionary, but not when I test if it is null in the if clause.

All the questions I found put the wrong number of arguments in a function call, but AFAIK there is nothing different between my two calls.

+6
source share
1 answer

I'm sure this is because your object type has an indexed property , and you pass null to the index parameter when GetValue is called.

Either remove the indexed property, or filter the indexed properties from your property variable, for example:

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

All Articles