2

I'm trying to attach a Generic list to a instance of a class using reflection, unlike when attaching simple objects method the PropertyInfo.SetValue(obj, value, index) is returning the exception {"Parameter count mismatch."} when value is an instance of List<SomeType> (as opposed to a string, int, bool or even a custom class).

That summery might not make much sense; The following may help to explain what I'm trying to do.

Say we are trying to populate the following class with reflection:

public class Foo
{

 public virtual int someInt {get; set;}
 public virtual IList<SomeClass> list {get; set;}

}

The method may look something like this:

public static T Parse<T>(HttpRequest request) where T : new()
{
   returnObj = new T();
   PropertyInfo[] properties = typeof(T).GetProperties();
   foreach (PropertyInfo p in properties)
   {
     // Get a meaningful property name
     string ins = System.Text.RegularExpressions.Regex.Replace(p.PropertyType.FullName, "([^,]*),.*$", "$1");
     switch(ins)
     {
        // populate int
        case "System.Int32":
           p.SetValue(returnObj, Int32.Parse(request[p.Name]) , null);
           break;



        // populate list
        case "System.Collections.Generic.IList`1[[SomeNamespace.Domain.SomeClass":
           IList<SomeClass> list = new List<SomeClass>();
           foreach (string s in request[p.Name].Split(','))
           {
              list.Add(new SomeClass(s));
           }
           // This will throw the exception 'Parameter count mismatch.'
           p.SetValue(returnObj, list, null);
           break;
      }
   }
   return returnObj;
}

However when trying to add an instance of List (IList) in this way an exception is thrown.

Edit: To clarify, If been over this method with a fine tooth comb(Breakpoints) (well, the one in the application, not exactly this one) and all the variables are populated as expected; until SetValue throws an exception; if anyone needs some more information, do ask.

Edit2: So I built a smaller application to test this (In order to upload it as an example); and I'm having trouble recreating my own issue; As many of you have suggested this works. I'll update this question with the issue when I manage to track it down. Its probably something trivial, as these things so often are (The original codebase is massive and therefore not appropriate for me to post). Thanks for all your help so far and my apologies for wasting your time.

11
  • 1
    are you sure p is the list? does your actual class expose an indexer with the same type as list? Commented Aug 28, 2012 at 15:56
  • I'm sure, I breakpointed it up and the Property names check out, aswell as the types. Commented Aug 28, 2012 at 15:59
  • 1
    Can you show a short but complete program demonstrating the problem? Commented Aug 28, 2012 at 16:14
  • whats the complete value of p.PropertyType.FullName before you run the Regex? Commented Aug 28, 2012 at 16:14
  • Can you confirm p.IsGeneric... is true? Commented Aug 28, 2012 at 16:15

2 Answers 2

2

Im running the code from your question with some minor changes to make it compile and it seems to work fine:

void Main()
{
    Parse<Foo>();
}

public static T Parse<T>() where T : new()
{
   var returnObj = new T();
   PropertyInfo[] properties = typeof(T).GetProperties();
   foreach (PropertyInfo p in properties)
   {
     // Get a meaningful property name
     string ins = p.PropertyType.Name;
     switch(ins)
     {
        // populate int
        case "Int32":
           p.SetValue(returnObj, 1 , null);
           break;

        // populate list
        case "IList`1":
           var list = new List<string>();
           // This will throw the exception 'Parameter count mismatch.'
           p.SetValue(returnObj, list, null);
           break;
      }
   }
   return returnObj;
}

public class Foo
{
 public virtual int someInt {get; set;}
 public virtual IList<string> list {get; set;}
}

If you change the Foo to have an indexer property that returns IList on the other hand you get the exception in your question:

public class Foo
{
 public virtual int someInt {get; set;}
 public virtual IList<string> this[int key] 
 {
    get{ return null; }
    set 
    {
    }
 }
}

Generates:

TargetParameterCountException: Parameter count mismatch.

Sign up to request clarification or add additional context in comments.

1 Comment

Turns out that the exception wasn't being thrown by that statement at all, Thanks for taking the time to test this.
1

Have you verified that the case

"System.Collections.Generic.IList`1[[SomeNamespace.Domain.SomeClass"

is being hit? When I pass in a List, even one created as an IList, GetType() gives me

System.Collections.Generic.List`1[SomeNamespace.Domain.SomeClass]

Would it potentially be more reliable to use:

typeOf(System.Collections.Generic.IList).isAssignableFrom(p.GetType())

2 Comments

It is being hit; breakpoints prove it. I got that string from inspecting the values of the p.PropertyType.FullName from the foreach, what you suggest would be neater tho.
You get the {"Parameter count mismatch."} error when passing in a List, correct? Or is that a different error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.