Esempio n. 1
0
        void OnEditTypeAssigned()
        {
            if (null != this.EditedType && this.EditedType.IsGenericTypeDefinition)
            {
                this.typeName.Text = TypeNameHelper.GetDisplayName(this.EditedType, false);

                Type[] generics = this.EditedType.GetGenericArguments();
                foreach (Type type in generics)
                {
                    Type         temp = type; // reference this temp variable instead of reference type in the anonymous delegate
                    TypeKeyValue tkv  = new TypeKeyValue(type, new Action <TypeKeyValue>(NotifyTypeChanged))
                    {
                        IsSelected = false,
                        Filter     = delegate(Type t)
                        {
                            if (!TypeUtilities.CanSubstituteGenericParameter(temp, t))
                            {
                                return(false);
                            }

                            return(this.Options == null ||
                                   this.Options.Filter == null ||
                                   this.Options.Filter(t));
                        },
                        MostRecentlyUsedTypes = this.Options != null ? this.Options.MostRecentlyUsedTypes : null,
                    };
                    string hintText = null;
                    if (this.Options != null && this.Options.HintTextMap.TryGetValue(type.Name, out hintText))
                    {
                        tkv.HintText = hintText;
                    }

                    this.GenericTypeMapping.Add(tkv);

                    if (this.Options == null || !this.Options.BrowseTypeDirectly)
                    {
                        tkv.BrowseTypeDirectly = false;
                        //this has to happen after the tkv is added GenericTypeMapping because:
                        //when TargetType is set, TypeResolver will try to resolve the generic type with this TargetType as type argument,
                        //and when resolvig the type, TypeResolver needs to know all the mappings
                        if (tkv.MostRecentlyUsedTypes == null)
                        {
                            if (tkv.Filter == null || tkv.Filter(typeof(int)))
                            {
                                tkv.TargetType = typeof(int);
                            }
                        }
                        else if (tkv.MostRecentlyUsedTypes.Contains(typeof(int)))
                        {
                            tkv.TargetType = typeof(int);
                        }
                        else if (tkv.MostRecentlyUsedTypes.Count > 0)
                        {
                            tkv.TargetType = tkv.MostRecentlyUsedTypes[0];
                        }
                    }
                }
            }
        }