English 中文(简体)
Adding Items to ListBox, RadioList, Combobox using reflection
原标题:

I m trying to add items to a listbox,combobox, radiolist using reflection. The code I have at the moment is as follows:

public static Control ConfigureControl(Control control, ControlConfig ctrlconf)
    {
        if (control is TextBox)
        {

            // ...
        }
        else
        {
            // get the properties of the control
            //

            Type controlType = control.GetType();

            PropertyInfo[] controlPropertiesArray = controlType.GetProperties();

            foreach (PropertyInfo controlProperty in controlPropertiesArray)
            {
                if (controlProperty.Name == "Items" && controlProperty.PropertyType == typeof(ListItemCollection))
                {
                    object instance = Activator.CreateInstance(controlProperty.PropertyType);
                    MethodInfo addMethod = controlProperty.PropertyType.GetMethod("Add", new Type[] { typeof(ListItem)} );
                    List<string> popValues = new List<string>(ctrlconf.PopulatedValues.Split( ; ));
                    if (popValues.Count.Equals(0))
                    {
                        throw new ArgumentException("No values found for control");
                    }
                    else
                    {
                        foreach (string val in popValues)
                        {
                            addMethod.Invoke(instance, new object[] { new ListItem(val, val) });
                        }

                    }

                }
            }
        }

        return control;

    }

The code above populates the listitemcollection which I have instantiated using Activator.CreateInstance, however I m not sure how to add it to the ListBox.

Any help would be great.

Thanks,

Peter

最佳回答

You don t need or want to instantiate the collection object: that s already done by the control. Instead, you need to get the existing collection object, then add to that:

if (controlProperty.Name == "Items" && controlProperty.PropertyType == typeof(ListItemCollection))
{
  object instance = controlProperty.GetValue(control, null);
  // ... now go on and add to the collection ...
}

However, as others have noted, this may not be the best way to approach the problem. Instead, consider implementing adapter or strategy for the various controls you want to support e.g. RadioButtonListItemAdder, ListControlItemAdder, etc., which all conform to a common interface. Each type of XxxItemAdder can implement its own strongly-typed code, suitable for the type of control it s responsible for adding items to. This might look something like the following:

public interface IItemAdder
{
  void AddItem(string value);
}

public class ListControlItemAdder : IItemAdder
{
  private readonly ListControl _listControl;

  public ListControlItemAdder(ListControl listControl)
  {
    _listControl = listControl;
  }

  public void AddItem(string value)
  {
    _listControl.Items.Add(value);  // or new ListItem(value, value) per your original code
  }
}

public class RadioButtonListItemAdder : IItemAdder
{
  // ...
  public void AddItem(string value)
  {
    // do whatever you have to do to add an item to a list of RadioButtons
  }
}

public static IItemAdder CreateItemAdderFor(Control control)
{
  if (control is ListControl)
    return new ListControlItemAdder((ListControl)control);
  else if (control is RadioButtonList)
    return new RadioButtonListItemAdder((RadioButtonList)control);
  // etc. to cover other cases
}

public static Control ConfigureControl(Control control, ...)
{
  // ... omitting code that looks like your existing code ...
  IItemAdder itemAdder = CreateItemAdderFor(control);
  foreach (string val in popValues)
    itemAdder.AddItem(val);
}

This is a really untidy implementation but hopefully gives you the idea of how you can separate out each of the individual control-specific implementations into small, nicely separated classes.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签