Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter count mismatch in property.GetValue() [duplicate]

Tags:

c#

I'm getting the

parameter count mismatch

error. It occurs at the if clause. My code:

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

It's strange because it works just fine when I add the property value to the dictionary, but not when I'm testing if it's null in the if clause.

All the questions I've found were putting an incorrect number of arguments into the function call, but AFAIK there's nothing different between my two calls.

like image 739
Firkamon Avatar asked Aug 05 '15 14:08

Firkamon


1 Answers

I'm pretty sure this is because your object type has an indexer "property" and you are passing null to the index parameter on the GetValue call.

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

var properties = obj.GetType().GetProperties()
                    .Where(p => p.GetIndexParameters().Length == 0);
like image 114
thepirat000 Avatar answered Nov 10 '22 07:11

thepirat000