4

I am getting a Parameter Count mismatch error which I don't understand.

I have the below code:

Type target = Type.GetType("CPS_Service." + DocumentType);

// Create an instance of my target class
instance = Activator.CreateInstance(target);

foreach (XElement pQ in PQData.Elements())
{
    try
    {
    // populate the member in the instance of the data class with the value from the MQ String
        if (target.GetProperty(pQ.Attribute("name").Value) != null)
        {
            target.GetProperty(pQ.Attribute("name").Value).SetValue(instance, pqRequest[Convert.ToInt32(pQ.Attribute("pos").Value)], null);
        }
    }
}

PropertyInfo[] properties = target.GetProperties();

foreach (PropertyInfo property in properties)
{
    DataColumn col = new DataColumn(property.Name);
    col.DataType = System.Type.GetType("System.String");
    col.DefaultValue = "";
    dt.Columns.Add(col);
}

DataRow dr = dt.NewRow();

foreach (PropertyInfo property in properties)
{
    string value = property.GetValue(instance).ToString();
    dr[property.Name.ToString()] = "";
}
dt.Rows.Add(dr);

return dt; //

so I am instantiating a generic class and populating it from a string array (taken from a tab-delimited string), then I need to either output a List or a datatable from the class instance

When populating the datarow dr for my datatable dt I am trying to get the value from the class:

string value = property.GetValue(instance, null).ToString();
dr[property.Name.ToString()] = "";

but on the line property.GetValue(instance).ToString(); I get the below error:

Parameter count mismatch

I have searched around and the other questions about this error do not apply...

Or would I be better off just casting my class to a List and returning that?

Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148
  • have to ask the question because one can not assume, so have you used the debugger..? also please show the full method signature for the class and or function that this code resides in – MethodMan Aug 21 '15 at 14:42
  • @MethodMan: Yes, using the debugger to step through, it's when I get to that line of code that the exception is thrown... the class is populated properly by the previous code....the setValue works perfectly, but cannot use getValue – Our Man in Bananas Aug 21 '15 at 14:45
  • what is the datatype of `dr[property.Name.ToString()]` at that point – MethodMan Aug 21 '15 at 14:47
  • take a look here looks like a similar issue the accepted answer seems to explain how to fix your issue http://stackoverflow.com/questions/12496791/targetexception-thrown-in-reflection-using-getvalue-in-nested-properties – MethodMan Aug 21 '15 at 14:49
  • it is a string, I want to set it to `value` – Our Man in Bananas Aug 21 '15 at 14:49
  • what is the value of instance at that point I want to try something on my end to emulate what you are trying to do .. what is the class structure also you are doing the following `return dt` please show the full method signature – MethodMan Aug 21 '15 at 14:53
  • @MethodMan: instance at that point is an object based on a class called *AccountTransfer* which has 9 string members. The classes are all similar to the one [on my question of yesterday about reflection](http://stackoverflow.com/questions/32124296/set-properties-of-data-class-using-reflection-in-constructor/32133249#32133249) – Our Man in Bananas Aug 21 '15 at 14:55

1 Answers1

10

If you're trying to get the value of all properties of a String (or any type that has an indexer), then you're going to have to have a special case to deal with the indexer. So if you wanted to get the value of that parameter you'd have to pass the object array of values with one parameter as the index value you wanted to get.

For example, property.GetValue(test, new object [] { 0 }); would get the value of the string at index 0. So if the string's value was "ABC", the result of would be 'A'.

The easiest thing would be just to skip the indexer. You can test if a property is an indexer by using property.GetIndexParameters().Any(). I thought you could skip this check by using the appropriate binding flag when calling GetProperties(), but if you can, I didn't see it.

If you want to skip the index in your code, then change:

PropertyInfo[] properties = target.GetProperties(); 

To:

var properties = target.GetProperties().Where(p => !p.GetIndexParameters().Any());
Daniel Gimenez
  • 18,530
  • 3
  • 50
  • 70