Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 76335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:39:26+00:00 2026-05-10T20:39:26+00:00

I want to pass an int list (List) as a declarative property to a

  • 0

I want to pass an int list (List) as a declarative property to a web user control like this:

<UC:MyControl runat='server' ModuleIds='1,2,3' /> 

I created a TypeConverter to do this:

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<int> list = new List<int>();             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) && value is List<int>)         {             List<int> list = (List<int>)value;             ConstructorInfo construcor = typeof(List<int>).GetConstructor(new Type[] { typeof(IEnumerable<int>) });             InstanceDescriptor id = new InstanceDescriptor(construcor, new object[] { list.ToArray() });             return id;         }         return base.ConvertTo(context, culture, value, destinationType);     } } 

And then added the attribute to my property:

[TypeConverter(typeof(IntListConverter))] public List<int> ModuleIds {     get { ... }; set { ... }; } 

But I get this error at runtime:

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.

My question is similar to one found here, but the solution does not solve my problem:

Update: I found a page which solved the first problem. I updated the code above to show my fixes. The added code is the CanConvertTo and ConvertTo methods. Now I get a different error.:

Object reference not set to an instance of an object.

This error seems to be indirectly caused by something in the ConvertTo method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:39:27+00:00Added an answer on May 10, 2026 at 8:39 pm

    After hooking a debugger into Cassini, I see that the null ref is actually coming from System.Web.Compilation.CodeDomUtility.GenerateExpressionForValue, which is basically trying to get an expression for the int[] array you pass into the List constructor. Since there’s no type descriptor for the int[] array, it fails (and throws a null ref in the process, instead of the ‘can’t generate property set exception’ that it should).

    I can’t figure out a built in way of getting a serializable value into a List<int>, so I just used a static method:

    class IntListConverter : TypeConverter {     public static List<int> FromString(string value) {        return new List<int>(           value            .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)            .Select(s => Convert.ToInt32(s))        );     }      public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {         if (destinationType == typeof(InstanceDescriptor)) {             List<int> list = (List<int>)value;             return new InstanceDescriptor(this.GetType().GetMethod('FromString'),                 new object[] { string.Join(',', list.Select(i => i.ToString()).ToArray()) }             );         }         return base.ConvertTo(context, culture, value, destinationType);     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Well you can try this DirectoryInfo sourcedinfo = new DirectoryInfo(@'E:\source');… May 11, 2026 at 10:15 am
  • added an answer You could load/save to a MemoryStream to solve your issue… May 11, 2026 at 10:15 am
  • added an answer The two previous answers got it right - I don't… May 11, 2026 at 10:15 am

Related Questions

I want to pass an int list (List) as a declarative property to a
I want to pass an integer value to a form in .Net so that
I want to pass an enum value as command parameter in WPF, using something
I want to be able to pass something into an SQL query to determine
I have an ASP.NET page. What I want to do is pass in an
I want to call an ASHX file and pass some query string variables from
I want to pass in the tType of a class to a function, and
I want to pass some parameters to my MVC UserControl like ShowTitle(bool) and the
I want to pass a variable to a UIButton action, for example NSString *string=@one;
I want to pass a byte[] to a method takes a IntPtr Parameter in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.