locked
System.Reflection.TargetParameterCountException occurred Message=Parameter count mismatch. RRS feed

  • Question

  • I have a c# Silverlight app and have a method that's throwing the exception:

    System.Reflection.TargetParameterCountException occurred  Message=Parameter count mismatch.

    Starting with these 2 lines of code:

    Type type = target.GetType();
    PropertyInfo[] props = type.GetProperties();


    Where target is a business object and props has 39 items in the array.  the next line fails on item 33 after succesfuly executing on the previous 32 items:

    object obj = props[i].GetValue(target, null);

    Here's a screenshot of the method

    and here's the data in props[]

    Any idea how to resolve this?

    Thank you for any help you can offer.

    Wednesday, June 24, 2015 8:30 PM

Answers

  • Hi,

    Based on TargetParameterCountException in MSDN, I got as below:

    The number of parameters in indexdoes not match the number of parameters the indexed property takes.

    In your case, one of the string's properties is the indexer for returning the character at the specified location. Thus, when you try to GetValue, the method expects an index but doesn't receive one, causing the exception.  

    To determine whether a property is indexed, please try to use the GetIndexParameters method. If the resulting array has 0 (zero) elements, the property is not indexed.

      try
                    {
                        int length=props[i].GetIndexParameters().Length;// check the value of length
                        object obj = props[i].GetValue(target,null);
                        if (convertvalueToStrings)
                        {
                            if (null == obj)
                            {
                                sortedList.Add(props[i].Name, "<null>");
                            }
                            else

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Thursday, June 25, 2015 5:46 AM
    • Marked as answer by moondaddy Friday, July 3, 2015 4:29 PM
    Thursday, June 25, 2015 5:44 AM

All replies

  • Hi,

    Based on TargetParameterCountException in MSDN, I got as below:

    The number of parameters in indexdoes not match the number of parameters the indexed property takes.

    In your case, one of the string's properties is the indexer for returning the character at the specified location. Thus, when you try to GetValue, the method expects an index but doesn't receive one, causing the exception.  

    To determine whether a property is indexed, please try to use the GetIndexParameters method. If the resulting array has 0 (zero) elements, the property is not indexed.

      try
                    {
                        int length=props[i].GetIndexParameters().Length;// check the value of length
                        object obj = props[i].GetValue(target,null);
                        if (convertvalueToStrings)
                        {
                            if (null == obj)
                            {
                                sortedList.Add(props[i].Name, "<null>");
                            }
                            else

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Thursday, June 25, 2015 5:46 AM
    • Marked as answer by moondaddy Friday, July 3, 2015 4:29 PM
    Thursday, June 25, 2015 5:44 AM
  • Hi Kristin,

    Thank you for this great information.  By reading you post, I was thinking if:

    length=props[i]

    when length is zero, then the exception would occur, but the opposite was true. 

    Please look at my test code in the screenshot and the results in the output window.  The last line in the output window is when the exception occurred.  So the loop is hitting every property in the class, then when it his theClass.Item the problem occurs.  By the way, the class doesn't have any property or object called Item and I think this is happening in a binding operation, but I cant see of my code in the callstack before this.

    so it looks like I should do something like this:

    if(length > 0)

    {

    // do workaround

    }

    else

    {

    // run normal code

    }

    Your comments?

    Thanks.

    Thursday, June 25, 2015 6:44 AM
  • Hi moondaddy,

    Sorry for my late reply.

    >>so it looks like I should do something like this:
    
    if(length > 0)
    
    {
    
    // do workaround
    
    }
    
    else
    {
    
    // run normal code
    
    }
    

    Yes,  based on the MSDN document, I got this.

    But I am curious about how do you call GetObjectKeyValueParis method? And what kind of parameter do you pass to object "Target"?  Could you post your code here if possible?

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 29, 2015 3:46 AM
  • Hi moondaddy,

    Have you fixed this issue now? If yes, would you mind post your solution here? It is helpful for others who has the same issue.

    Regards & Kristin



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Friday, July 3, 2015 9:45 AM
  • Hi Kristin,

    We resolved it using your information by testing the data with this first:

    if (props[i].GetIndexParameters().Length > 0)

    If this is true, then the old code would get an exception, so we added a workaround here.

    Its hard to explain what gets passed in as we developed an ObjectHelper class (that uses this code) which our business objects derive from and it overrides the ToString method and has a lot of things that help us with tracing.  This exception was happing from a databinding that was not happening in our code so I could never see where it came from in the callstack.

    So the short answer is, your recommendation of using GetIndexParameters was the fix.  thanks.

    Friday, July 3, 2015 4:29 PM
  • Hi Kristin,

    We resolved it using your information by testing the data with this first:

    if (props[i].GetIndexParameters().Length > 0)

    If this is true, then the old code would get an exception, so we added a workaround here.

    Its hard to explain what gets passed in as we developed an ObjectHelper class (that uses this code) which our business objects derive from and it overrides the ToString method and has a lot of things that help us with tracing.  This exception was happing from a databinding that was not happening in our code so I could never see where it came from in the callstack.

    So the short answer is, your recommendation of using GetIndexParameters was the fix.  thanks.

    Hi moondaddy,

    Thanks for coming back and I am glad to know using GetIndexParameters would help.

    Regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, July 6, 2015 9:48 AM