Ejemplo n.º 1
0
        /// <summary>
        /// Reads metadata from the type.
        /// </summary>
        /// <param name="type">The type.</param>
        public static UserTypeMetadata[] ReadFromType(Type type)
        {
            UserTypeAttribute[] attributes = type.GetCustomAttributes <UserTypeAttribute>(false).ToArray();
            bool derivedFromUserType       = IsDerivedFrom(type, typeof(UserType));

            /* #fixme temp workaround for genertic types
             * if (type.IsGenericType)
             * {
             *  foreach(Type genericArgument in type.GetGenericArguments())
             *  {
             *      UserTypeAttribute genericAttribute = genericArgument.GetCustomAttribute<UserTypeAttribute>();
             *      if (genericAttribute != null)
             *      {
             *          string moduleName = genericAttribute.ModuleName;
             *          // #fixme temp workaround
             *          string typeName = type.Name + "<>";
             *
             *          return new UserTypeMetadata(moduleName, typeName, type);
             *      }
             *  }
             * }
             */

            if (attributes.Length > 0 && !derivedFromUserType)
            {
                throw new Exception(string.Format("Type {0} has defined UserTypeAttribute, but it does not inherit UserType", type.FullName));
            }
            else if (derivedFromUserType)
            {
                UserTypeMetadata[] metadata = new UserTypeMetadata[attributes.Length];

                for (int i = 0; i < metadata.Length; i++)
                {
                    UserTypeAttribute attribute  = attributes[i];
                    string            moduleName = attribute != null ? attribute.ModuleName : null;
                    string            typeName   = attribute != null ? attribute.TypeName : !type.IsGenericType ? type.Name : type.Name.Substring(0, type.Name.IndexOf('`')) + "<>"; // TODO: Form better name for generics type

                    metadata[i] = new UserTypeMetadata(moduleName, typeName, type);
                }

                return(metadata);
            }

            return(new UserTypeMetadata[0]);
        }
 private Control CreateControlIfPropertyIsBoolean(PropertyInfo property, UserTypeAttribute attr)
 {
     var booleanAttribute = attr as BooleanAttribute;
     if (booleanAttribute != null)
     {
         var value = settings.GetPropertyValue<bool>(property);
         var checkBox = new CheckBox {IsChecked = value, Name = property.Name};
         return checkBox;
     }
     return null;
 }
        private Control CreateControlIfPropertyIsTextual(PropertyInfo property, UserTypeAttribute attr)
        {
            var textAttribute = attr as TextAttribute;
            if (textAttribute != null)
            {
                var value = settings.GetPropertyValue<string>(property);
                var textField = new TextBox
                                    {
                                        Name = property.Name,
                                        Text = (value ?? ""),
                                        MaxLength = textAttribute.MaxLength,

                                    };
                if (textAttribute.LineCount > 1)
                {
                    textField.MinLines = textAttribute.LineCount;
                    textField.TextWrapping = TextWrapping.Wrap;
                    textField.AcceptsReturn = true;
                    textField.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                }
                return textField;
            }
            return null;
        }
 private Control CreateControlIfPropertyIsNumeric(PropertyInfo property, UserTypeAttribute attr)
 {
     var numericAttribute = attr as NumericAttribute;
     if (numericAttribute != null)
     {
         var value = settings.GetPropertyValue<int>(property);
         var numericField = new NumericUpDown
                                {
                                    Name = property.Name,
                                    Minimum = numericAttribute.MinValue,
                                    Maximum = numericAttribute.MaxValue,
                                    Change = numericAttribute.Increment,
                                    Value = value
                                };
         return numericField;
     }
     return null;
 }
        private Control CreateControlIfPropertyIsEnum(PropertyInfo property, UserTypeAttribute attr)
        {
            var enumAttribute = attr as EnumAttribute;
            if (enumAttribute != null)
            {
                var value = Convert.ChangeType(settings.GetPropertyValue<object>(property), enumAttribute.EnumType);
                var comboField = new EnumComboBoxWpf
                                     {
                                         Name = property.Name,
                                     };
                comboField.SetEnum(enumAttribute.EnumType);
                if (value!=null) comboField.SelectedEnumItem = value;

                return comboField;
            }
            return null;
        }