Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5848 for trunk/sources


Ignore:
Timestamp:
03/28/11 18:07:43 (14 years ago)
Author:
mkommend
Message:

#1049: Adapted TypeSelector to respect the new() constraint on generic types.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core.Views/3.3/TypeSelector.cs

    r5837 r5848  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Reflection;
    2526using System.Text;
    2627using System.Windows.Forms;
     
    114115          pluginNode.Tag = plugin;
    115116
    116           var types = from t in ApplicationManager.Manager.GetTypes(BaseType, plugin, false)
     117          var types = from t in ApplicationManager.Manager.GetTypes(BaseType, plugin, ShowNotInstantiableTypes)
    117118                      orderby t.Name ascending
    118119                      select t;
     
    120121            bool valid = true;
    121122            valid = valid && (ShowGenericTypes || !type.ContainsGenericParameters);
    122             valid = valid && (ShowNotInstantiableTypes || !type.IsAbstract);
    123             valid = valid && (ShowNotInstantiableTypes || !type.IsInterface);
    124             valid = valid && (ShowNotInstantiableTypes || !type.HasElementType);
    125123            valid = valid && (ShowNotInstantiableTypes || type.GetConstructor(Type.EmptyTypes) != null); //check for public default ctor
    126124            if (valid) {
     
    132130              else if (type.ContainsGenericParameters) typeNode.ImageIndex = 3;
    133131              else if (imageList.Images.ContainsKey(type.FullName)) typeNode.ImageIndex = imageList.Images.IndexOfKey(type.FullName);
    134               else if (typeof(IItem).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract) {
    135                 try {
    136                   IItem item = (IItem)Activator.CreateInstance(type);
    137                   imageList.Images.Add(type.FullName, item.ItemImage);
    138                   typeNode.ImageIndex = imageList.Images.IndexOfKey(type.FullName);
    139                 }
    140                 catch (Exception) { }
     132              else if (typeof(IItem).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract && type.GetConstructor(Type.EmptyTypes) != null) {
     133                IItem item = (IItem)Activator.CreateInstance(type);
     134                imageList.Images.Add(type.FullName, item.ItemImage);
     135                typeNode.ImageIndex = imageList.Images.IndexOfKey(type.FullName);
    141136              }
    142137              typeNode.SelectedImageIndex = typeNode.ImageIndex;
     
    258253      Type param = typeParametersListView.SelectedItems[0].Tag as Type;
    259254      Type[] contraints = param.GetGenericParameterConstraints();
    260       typeSelectorDialog.TypeSelector.Configure(typeof(IItem), true, true);
     255      bool showNotInstantiableTypes = !param.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint);
     256      typeSelectorDialog.TypeSelector.Configure(typeof(IItem), showNotInstantiableTypes, true);
    261257
    262258      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
Note: See TracChangeset for help on using the changeset viewer.