예제 #1
0
파일: Utils.cs 프로젝트: akrisiun/AiLib
 public static TypeConverter GetPropertyTypeConverter(PropertyInfo propertyInfo)
 {
     foreach (object attribute in propertyInfo.GetCustomAttributes(typeof(TypeConverterAttribute), false))
     {
         TypeConverterAttribute attr = attribute as TypeConverterAttribute;
         if (!attr.IsDefaultAttribute())
         {
             try
             {
                 var converter = Activator.CreateInstance(Type.GetType(attr.ConverterTypeName))
                                 as TypeConverter;
                 return(converter);
             }
             catch { }
         }
     }
     return(null);
 }
예제 #2
0
        /// <summary>
        /// Factory method that creates the option item using the provided parameters.
        /// </summary>
        protected virtual IOptionItem CreateItem(IOptionBuilderContext context, PropertyInfo propertyInfo, Type type,
                                                 string description, object value)
        {
            IOptionItem item = null;

//      if (type.IsEnum) {
//        Type genericType = typeof (GenericOptionItem<>);
//        Type newType = genericType.MakeGenericType(type);
//        item = newType.GetConstructor(new Type[] {typeof (string)}).Invoke(new object[] {description}) as IOptionItem;
//      } else if (type == typeof (Color)) {
//        item = new ColorOptionItem(description);
            //  } else
            if (type.IsGenericType && type.GetGenericTypeDefinition().IsAssignableFrom((typeof(ICollection <>))))
            {
                Type[] types = type.GetGenericArguments();
                if (types.Length == 1)
                {
                    Type collectionItemType       = types[0];
                    Type collectionBaseType       = typeof(ICollection <>);
                    Type collectionType           = collectionBaseType.MakeGenericType(collectionItemType);
                    Type collectionOptionItemType = typeof(CollectionOptionItem <>);
                    item = collectionOptionItemType.MakeGenericType(collectionItemType).GetConstructor(
                        new Type[] { typeof(string), collectionType }).Invoke(new object[] { description, value }) as
                           IOptionItem;
                }
            }

            if (item == null)
            {
                if (type.IsValueType)
                {
                    if (IsNullable(type))
                    {
                        item = new OptionItem()
                        {
                            Type = Nullable.GetUnderlyingType(type), Name = description
                        };
                        item.Attributes[OptionItem.SupportNullValueAttribute] = true;
                    }
                    else if (IsNullable(propertyInfo.PropertyType))
                    {
                        item = new OptionItem()
                        {
                            Type = type, Name = description
                        };
                        item.Attributes[OptionItem.SupportNullValueAttribute] = true;
                    }
                    else
                    {
                        item = new OptionItem()
                        {
                            Type = type, Name = description
                        };
                        item.Attributes[OptionItem.SupportNullValueAttribute] = false;
                    }
                }
                else
                {
                    item = new OptionItem()
                    {
                        Type = type, Name = description
                    };
                }
            }
            var customAttributes = Attribute.GetCustomAttributes(propertyInfo);

            foreach (var attribute in customAttributes.OfType <OptionItemAttributeAttribute>())
            {
                item.Attributes[attribute.Name] = attribute.Value;
            }

            TypeConverterAttribute converter = GetAttribute <TypeConverterAttribute>(propertyInfo);

            if (converter != null && !converter.IsDefaultAttribute())
            {
                try {
                    Type typeConverter = Type.GetType(converter.ConverterTypeName);
                    if (typeConverter != null)
                    {
                        item.Attributes[OptionItem.CustomTypeConverterAttribute] = typeConverter;
                    }
                } catch (Exception e) {
                    Trace.WriteLine("Could not load custom type converter " + e.Message);
                }
            }

            return(item);
        }
        /// <summary>
        /// Factory method that creates the option item using the provided parameters.
        /// </summary>
        protected virtual IOptionItem CreateItem(IOptionBuilderContext context, PropertyInfo propertyInfo, Type type,
                                                 string description, object value)
        {
            IOptionItem item = null;

            if (type.IsEnum)
            {
                Type genericType = typeof(GenericOptionItem <>);
                Type newType     = genericType.MakeGenericType(type);
                item = newType.GetConstructor(new Type[] { typeof(string) }).Invoke(new object[] { description }) as IOptionItem;
            }
            else if (type == typeof(Color))
            {
                item = new ColorOptionItem(description);
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition().IsAssignableFrom((typeof(ICollection <>))))
            {
                Type[] types = type.GetGenericArguments();
                if (types.Length == 1)
                {
                    Type collectionItemType       = types[0];
                    Type collectionBaseType       = typeof(ICollection <>);
                    Type collectionType           = collectionBaseType.MakeGenericType(collectionItemType);
                    Type collectionOptionItemType = typeof(CollectionOptionItem <>);
                    item = collectionOptionItemType.MakeGenericType(collectionItemType).GetConstructor(
                        new Type[] { typeof(string), collectionType }).Invoke(new object[] { description, value }) as
                           IOptionItem;
                }
            }

            if (item == null)
            {
                if (type == typeof(double))
                {
                    item = new DoubleOptionItem(description);
                }
                else if (type == typeof(int))
                {
                    item = new IntOptionItem(description);
                }
                else if (type == typeof(float))
                {
                    item = new FloatOptionItem(description);
                }
                else if (type == typeof(bool))
                {
                    item = new BoolOptionItem(description);
                }
                else if (type == typeof(string))
                {
                    item = new StringOptionItem(description);
                }
                else
                {
                    Type genericType = typeof(GenericOptionItem <>);
                    Type newType     = genericType.MakeGenericType(type);
                    item =
                        newType.GetConstructor(new Type[] { typeof(string) }).Invoke(new object[] { description }) as IOptionItem;
                }
            }
            if (item != null)
            {
                TypeConverterAttribute converter = GetAttribute <TypeConverterAttribute>(propertyInfo);
                if (converter != null && !converter.IsDefaultAttribute())
                {
                    try {
                        Type typeConverter = Type.GetType(converter.ConverterTypeName);
                        if (typeConverter != null)
                        {
                            item.SetAttribute(OptionItem.CUSTOM_CONVERTER_ATTRIBUTE, typeConverter);
                        }
                    } catch (Exception e) {
                        Trace.WriteLine("Could not load custom type converter " + e.Message);
                    }
                }
                //by default, value types are not nullable
                if (type.IsValueType)
                {
                    item.SetAttribute(OptionItem.SUPPORT_NULL_VALUE_ATTRIBUTE, false);
                }
            }


            return(item);
        }