title: Passing int list as a parameter to a web user control
body: <p>I want to pass an int list (List) as a declarative property to a web user control like this:</p>
  
  <pre><code>&lt;UC:MyControl runat="server" ModuleIds="1,2,3" /&gt;
  </code></pre>
  
  <p>I created a TypeConverter to do this:</p>
  
  <pre><code>public class IntListConverter : System.ComponentModel.TypeConverter
  {
      public override bool CanConvertFrom(
             System.ComponentModel.ITypeDescriptorContext context, 
             Type sourceType)
      {
          if (sourceType == typeof(string)) return true;
          return base.CanConvertFrom(context, sourceType);
      }
      public override object ConvertFrom(
        System.ComponentModel.ITypeDescriptorContext context, 
        System.Globalization.CultureInfo culture, object value)
      {
          if (value is string)
          {
              string[] v = ((string)value).Split(
                  new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
              List&lt;int&gt; list = new List&lt;int&gt;();
              foreach (string s in vals)
              {
                  list.Add(Convert.ToInt32(s));
              }
              return list
          }
          return base.ConvertFrom(context, culture, value);
      }
      public override bool CanConvertTo(ITypeDescriptorContext context,
        Type destinationType)
      {
          if (destinationType == typeof(InstanceDescriptor)) return true;
          return base.CanConvertTo(context, destinationType);
      }
      public override object ConvertTo(ITypeDescriptorContext context,
        System.Globalization.CultureInfo culture, object value, Type destinationType)
      {
          if (destinationType == typeof(InstanceDescriptor) &amp;&amp; value is List&lt;int&gt;)
          {
              List&lt;int&gt; list = (List&lt;int&gt;)value;
              ConstructorInfo construcor = typeof(List&lt;int&gt;).GetConstructor(new Type[] { typeof(IEnumerable&lt;int&gt;) });
              InstanceDescriptor id = new InstanceDescriptor(construcor, new object[] { list.ToArray() });
              return id;
          }
          return base.ConvertTo(context, culture, value, destinationType);
      }
  }
  </code></pre>
  
  <p>And then added the attribute to my property:</p>
  
  <pre><code>[TypeConverter(typeof(IntListConverter))]
  public List&lt;int&gt; ModuleIds
  {
      get { ... }; set { ... };
  }
  </code></pre>
  
  <p>But I get this error at runtime:</p>
  
  <p><code>Unable to generate code for a value of type 'System.Collections.Generic.List'1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'. This error occurred while trying to generate the property value for ModuleIds.</code></p>
  
  <p>My question is similar to one found <a href="https://stackoverflow.com/questions/116797/passing-int-array-as-parameter-in-web-user-control">here</a>, but the solution does not solve my problem:</p>
  
  <p><strong>Update:</strong> I found a page which solved the first problem.  I updated the code above to show my fixes.  The added code is the <code>CanConvertTo</code> and <code>ConvertTo</code> methods.  Now I get a different error.:</p>
  
  <p><code>Object reference not set to an instance of an object.</code></p>
  
  <p>This error seems to be indirectly caused by something in the <code>ConvertTo</code> method.</p>