コード例 #1
0
        /// <summary>
        /// Adds a new property.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="type">The type.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <param name="readOnly">if set to <c>true</c> the property is read only.</param>
        /// <param name="sortOrder">The property sort order.</param>
        /// <param name="attributes">The property custom attributes or null.</param>
        /// <returns>
        /// An instance of the DynamicObjectProperty type.
        /// </returns>
        public virtual DynamicObjectProperty AddProperty(string name, Type type, object defaultValue, bool readOnly, int sortOrder, Attribute[] attributes)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (_properties.Find(x => x.Name == name) != null)
            {
                throw new ArgumentException("Property '" + name + "' is already defined", "name");
            }

            List <Attribute> newAtts;

            if (attributes != null)
            {
                newAtts = new List <Attribute>(attributes);
            }
            else
            {
                newAtts = new List <Attribute>();
            }

            newAtts.RemoveAll(a => a is ReadOnlyAttribute);
            newAtts.RemoveAll(a => a is DefaultValueAttribute);

            if (readOnly)
            {
                newAtts.Add(new ReadOnlyAttribute(true));
            }
            newAtts.Add(new DefaultValueAttribute(defaultValue));

            DynamicObjectProperty dop = CreateProperty(name, type, newAtts.ToArray());

            dop.SortOrder = sortOrder;
            _properties.Add(dop);
            return(dop);
        }
コード例 #2
0
        /// <summary>
        /// Adds a new property.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="type">The type.</param>
        /// <param name="attributes">The property custom attributes or null.</param>
        /// <returns>An instance of the DynamicObjectProperty type.</returns>
        public virtual DynamicObjectProperty AddProperty(string name, Type type, IEnumerable <Attribute> attributes)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (_properties.Find(x => x.Name == name) != null)
            {
                throw new ArgumentException("Property '" + name + "' is already defined", "name");
            }

            DynamicObjectProperty dop = CreateProperty(name, type, attributes);

            _properties.Add(dop);
            return(dop);
        }