/// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        /// <param name="attributeRegistry">The attribute registry.</param>
        /// <param name="type">The type.</param>
        /// <param name="namingConvention">The naming convention.</param>
        /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception>
        public PrimitiveDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention)
			: base(attributeRegistry, type, false, namingConvention)
		{
			if (!IsPrimitive(type))
				throw new ArgumentException("Type [{0}] is not a primitive");

            // Handle remap for enum items
            if (type.IsEnum)
            {
                foreach (var member in type.GetFields(BindingFlags.Public|BindingFlags.Static))
                {
                    var attributes = attributeRegistry.GetAttributes(member);
                    foreach (var attribute in attributes)
                    {
                        var yamlRemap = attribute as YamlRemapAttribute;
                        if (yamlRemap != null)
                        {
                            if (enumRemap == null)
                            {
                                enumRemap = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
                            }
                            enumRemap[yamlRemap.Name] = member.GetValue(null);
                        }
                    }
                }
            }
		}
        /// <summary>
        /// Initializes a new instance of the <see cref="CollectionDescriptor" /> class.
        /// </summary>
        /// <param name="attributeRegistry">The attribute registry.</param>
        /// <param name="type">The type.</param>
        /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param>
        /// <param name="namingConvention">The naming convention.</param>
        /// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.ICollection;type</exception>
        public CollectionDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
			: base(attributeRegistry, type, emitDefaultValues, namingConvention)
		{
			if (!IsCollection(type))
				throw new ArgumentException("Expecting a type inheriting from System.Collections.ICollection", "type");

			// Gets the element type
			var collectionType = type.GetInterface(typeof(IEnumerable<>));
			ElementType = (collectionType != null) ? collectionType.GetGenericArguments()[0] : typeof(object);

			// implements ICollection<T> 
			Type itype;
			if ((itype = type.GetInterface(typeof(ICollection<>))) != null)
			{
				var add = itype.GetMethod("Add", new [] { ElementType });
				CollectionAddFunction = (obj, value) => add.Invoke(obj, new [] { value });
				var countMethod = itype.GetProperty("Count").GetGetMethod();
				GetCollectionCountFunction = o => (int)countMethod.Invoke(o, null);
				var isReadOnly = itype.GetProperty("IsReadOnly").GetGetMethod();
				IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null);
			    isKeyedCollection = type.ExtendsGeneric(typeof (KeyedCollection<,>));
			}
			// implements IList 
			else if (typeof (IList).IsAssignableFrom(type))
			{
				CollectionAddFunction = (obj, value) => ((IList) obj).Add(value);
				GetCollectionCountFunction = o => ((IList) o).Count;
				IsReadOnlyFunction = obj => ((IList) obj).IsReadOnly;
			}
		}
	    /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        /// <param name="attributeRegistry">The attribute registry.</param>
        /// <param name="type">The type.</param>
        /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param>
        /// <param name="namingConvention">The naming convention.</param>
        /// <exception cref="System.ArgumentNullException">type</exception>
        /// <exception cref="YamlException">type</exception>
		public ObjectDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
		{
			if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry");
			if (type == null) throw new ArgumentNullException("type");
            if (namingConvention == null) throw new ArgumentNullException("namingConvention");

            this.memberNamingConvention = namingConvention;
            this.emitDefaultValues = emitDefaultValues;
			this.AttributeRegistry = attributeRegistry;
			this.type = type;

            attributes = AttributeRegistry.GetAttributes(type);

            this.style = YamlStyle.Any;
            foreach (var attribute in attributes)
            {
                var styleAttribute = attribute as YamlStyleAttribute;
                if (styleAttribute != null)
                {
                    style = styleAttribute.Style;
                    continue;
                }
                if (attribute is CompilerGeneratedAttribute)
                {
                    this.IsCompilerGenerated = true;
                }
            }
		}
Exemple #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        /// <param name="attributeRegistry">The attribute registry.</param>
        /// <param name="type">The type.</param>
        /// <param name="namingConvention">The naming convention.</param>
        /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception>
        public PrimitiveDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention)
            : base(attributeRegistry, type, false, namingConvention)
        {
            if (!IsPrimitive(type))
            {
                throw new ArgumentException("Type [{0}] is not a primitive");
            }

            // Handle remap for enum items
            if (type.GetTypeInfo().IsEnum)
            {
                foreach (var member in type.GetFields(BindingFlags.Public | BindingFlags.Static))
                {
                    var attributes = attributeRegistry.GetAttributes(member);
                    foreach (var attribute in attributes)
                    {
                        var yamlRemap = attribute as YamlRemapAttribute;
                        if (yamlRemap != null)
                        {
                            if (enumRemap == null)
                            {
                                enumRemap = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);
                            }
                            enumRemap[yamlRemap.Name] = member.GetValue(null);
                        }
                    }
                }
            }
        }
        public PrimitiveDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
            : base(factory, type, emitDefaultValues, namingConvention)
        {
            if (!IsPrimitive(type))
                throw new ArgumentException("Type [{0}] is not a primitive");

            // Handle remap for enum items
            if (type.IsEnum)
            {
                foreach (var member in type.GetFields(BindingFlags.Public | BindingFlags.Static))
                {
                    var attributes = AttributeRegistry.GetAttributes(member);
                    foreach (var attribute in attributes)
                    {
                        var aliasAttribute = attribute as DataAliasAttribute;
                        if (aliasAttribute != null)
                        {
                            if (enumRemap == null)
                            {
                                enumRemap = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
                            }
                            enumRemap[aliasAttribute.Name] = member.GetValue(null);
                        }
                    }
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        public ObjectDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (namingConvention == null)
            {
                throw new ArgumentNullException(nameof(namingConvention));
            }

            this.factory           = factory;
            Type                   = type;
            IsCompilerGenerated    = AttributeRegistry.GetAttribute <CompilerGeneratedAttribute>(type) != null;
            this.emitDefaultValues = emitDefaultValues;
            NamingConvention       = namingConvention;

            Attributes = AttributeRegistry.GetAttributes(type);

            Style = DataStyle.Any;
            foreach (var attribute in Attributes)
            {
                var styleAttribute = attribute as DataStyleAttribute;
                if (styleAttribute != null)
                {
                    Style = styleAttribute.Style;
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        /// <param name="factory">The factory.</param>
        /// <param name="type">The type.</param>
        /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception>
        public NullableDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
            : base(factory, type, emitDefaultValues, namingConvention)
        {
            if (!IsNullable(type))
                throw new ArgumentException("Type [{0}] is not a primitive");

            UnderlyingType = Nullable.GetUnderlyingType(type);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        /// <param name="attributeRegistry">The attribute registry.</param>
        /// <param name="type">The type.</param>
        /// <param name="namingConvention">The naming convention.</param>
        /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception>
        public NullableDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention)
            : base(attributeRegistry, type, false, namingConvention)
        {
            if (!IsNullable(type))
                throw new ArgumentException("Type [{0}] is not a primitive");

            UnderlyingType = Nullable.GetUnderlyingType(type);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="TypeDescriptorFactory" /> class.
        /// </summary>
        /// <param name="attributeRegistry">The attribute registry.</param>
        /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param>
        /// <param name="namingConvention">The naming convention.</param>
        /// <exception cref="System.ArgumentNullException">attributeRegistry</exception>
        public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues, IMemberNamingConvention namingConvention)
		{
			if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry");
            if (namingConvention == null) throw new ArgumentNullException("namingConvention");
            this.namingConvention = namingConvention;
			this.emitDefaultValues = emitDefaultValues;
			this.attributeRegistry = attributeRegistry;
		}
 public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues, IMemberNamingConvention namingConvention, IComparer<object> keyComparer)
 {
     if (attributeRegistry == null) throw new ArgumentNullException(nameof(attributeRegistry));
     this.keyComparer = keyComparer;
     AttributeRegistry = attributeRegistry;
     this.emitDefaultValues = emitDefaultValues;
     this.namingConvention = namingConvention;
 }
Exemple #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        /// <param name="attributeRegistry">The attribute registry.</param>
        /// <param name="type">The type.</param>
        /// <param name="namingConvention">The naming convention.</param>
        /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception>
        public NullableDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention)
            : base(attributeRegistry, type, false, namingConvention)
        {
            if (!IsNullable(type))
            {
                throw new ArgumentException("Type [{0}] is not a primitive");
            }

            UnderlyingType = Nullable.GetUnderlyingType(type);
        }
Exemple #12
0
 public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues, IMemberNamingConvention namingConvention, IComparer <object> keyComparer)
 {
     if (attributeRegistry == null)
     {
         throw new ArgumentNullException(nameof(attributeRegistry));
     }
     this.keyComparer       = keyComparer;
     AttributeRegistry      = attributeRegistry;
     this.emitDefaultValues = emitDefaultValues;
     this.namingConvention  = namingConvention;
 }
        public ArrayDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
            : base(factory, type, emitDefaultValues, namingConvention)
        {
            if (!type.IsArray) throw new ArgumentException(@"Expecting array type", nameof(type));

            if (type.GetArrayRank() != 1)
            {
                throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".ToFormat(type.GetArrayRank(), type.FullName));
            }

            ElementType = type.GetElementType();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        /// <param name="attributeRegistry">The attribute registry.</param>
        /// <param name="type">The type.</param>
        /// <param name="namingConvention">The naming convention.</param>
        /// <exception cref="System.ArgumentException">Expecting arrat type;type</exception>
        public ArrayDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention)
            : base(attributeRegistry, type, false, namingConvention)
        {
            if (!type.IsArray) throw new ArgumentException("Expecting array type", "type");

            if (type.GetArrayRank() != 1)
            {
                throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".DoFormat(type.GetArrayRank(), type.FullName));
            }

            elementType = type.GetElementType();
            listType = typeof(List<>).MakeGenericType(ElementType);
            toArrayMethod = listType.GetMethod("ToArray");
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeDescriptorFactory" /> class.
 /// </summary>
 /// <param name="attributeRegistry">The attribute registry.</param>
 /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param>
 /// <param name="namingConvention">The naming convention.</param>
 /// <exception cref="System.ArgumentNullException">attributeRegistry</exception>
 public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues, IMemberNamingConvention namingConvention)
 {
     if (attributeRegistry == null)
     {
         throw new ArgumentNullException("attributeRegistry");
     }
     if (namingConvention == null)
     {
         throw new ArgumentNullException("namingConvention");
     }
     this.namingConvention  = namingConvention;
     this.emitDefaultValues = emitDefaultValues;
     this.attributeRegistry = attributeRegistry;
 }
Exemple #16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        /// <param name="attributeRegistry">The attribute registry.</param>
        /// <param name="type">The type.</param>
        /// <param name="namingConvention">The naming convention.</param>
        /// <exception cref="System.ArgumentException">Expecting arrat type;type</exception>
        public ArrayDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention)
            : base(attributeRegistry, type, false, false, namingConvention)
        {
            if (!type.IsArray)
            {
                throw new ArgumentException("Expecting array type", "type");
            }

            if (type.GetArrayRank() != 1)
            {
                throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".DoFormat(type.GetArrayRank(), type.FullName));
            }

            elementType = type.GetElementType();
        }
Exemple #17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        public ObjectDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (namingConvention == null)
            {
                throw new ArgumentNullException(nameof(namingConvention));
            }

            this.factory           = factory;
            Type                   = type;
            IsCompilerGenerated    = AttributeRegistry.GetAttribute <CompilerGeneratedAttribute>(type) != null;
            this.emitDefaultValues = emitDefaultValues;
            NamingConvention       = namingConvention;

            Attributes = AttributeRegistry.GetAttributes(type);

            Style = DataStyle.Any;
            foreach (var attribute in Attributes)
            {
                var styleAttribute = attribute as DataStyleAttribute;
                if (styleAttribute != null)
                {
                    Style = styleAttribute.Style;
                }
            }

            // Get DefaultMemberMode from DataContract
            DefaultMemberMode = DataMemberMode.Default;
            var currentType = type;

            while (currentType != null)
            {
                var dataContractAttribute = AttributeRegistry.GetAttribute <DataContractAttribute>(currentType);
                if (dataContractAttribute != null && (dataContractAttribute.Inherited || currentType == type))
                {
                    DefaultMemberMode = dataContractAttribute.DefaultMemberMode;
                    break;
                }
                currentType = currentType.BaseType;
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        /// <param name="attributeRegistry">The attribute registry.</param>
        /// <param name="type">The type.</param>
        /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param>
        /// <param name="ignoreGetters">If set to <c>true</c>, the properties without setters will be ignored.</param>
        /// <param name="namingConvention">The naming convention.</param>
        /// <exception cref="System.ArgumentNullException">type</exception>
        /// <exception cref="YamlException">type</exception>
        public ObjectDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, bool ignoreGetters, IMemberNamingConvention namingConvention)
        {
            if (attributeRegistry == null)
            {
                throw new ArgumentNullException("attributeRegistry");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (namingConvention == null)
            {
                throw new ArgumentNullException("namingConvention");
            }

            this.memberNamingConvention = namingConvention;
            this.emitDefaultValues      = emitDefaultValues;
            this.ignoreGetters          = ignoreGetters;
            this.AttributeRegistry      = attributeRegistry;
            this.type = type;

            attributes = AttributeRegistry.GetAttributes(type.GetTypeInfo());

            this.style = YamlStyle.Any;
            foreach (var attribute in attributes)
            {
                var styleAttribute = attribute as YamlStyleAttribute;
                if (styleAttribute != null)
                {
                    style = styleAttribute.Style;
                    continue;
                }
                if (attribute is CompilerGeneratedAttribute)
                {
                    this.IsCompilerGenerated = true;
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        public ObjectDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
        {
            if (factory == null) throw new ArgumentNullException(nameof(factory));
            if (type == null) throw new ArgumentNullException(nameof(type));
            if (namingConvention == null) throw new ArgumentNullException(nameof(namingConvention));

            this.factory = factory;
            Type = type;
            IsCompilerGenerated = AttributeRegistry.GetAttribute<CompilerGeneratedAttribute>(type) != null;
            this.emitDefaultValues = emitDefaultValues;
            NamingConvention = namingConvention;

            Attributes = AttributeRegistry.GetAttributes(type);

            Style = DataStyle.Any;
            foreach (var attribute in Attributes)
            {
                var styleAttribute = attribute as DataStyleAttribute;
                if (styleAttribute != null)
                {
                    Style = styleAttribute.Style;
                }
            }
        }
Exemple #20
0
        public PrimitiveDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
            : base(factory, type, emitDefaultValues, namingConvention)
        {
            if (!IsPrimitive(type))
            {
                throw new ArgumentException("Type [{0}] is not a primitive");
            }

            // Handle remap for enum items
            if (type.IsEnum)
            {
                foreach (var member in type.GetFields(BindingFlags.Public | BindingFlags.Static))
                {
                    var attributes = AttributeRegistry.GetAttributes(member);
                    foreach (var attribute in attributes)
                    {
                        var aliasAttribute = attribute as DataAliasAttribute;
                        if (aliasAttribute != null)
                        {
                            if (enumRemap == null)
                            {
                                enumRemap = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);
                            }
                            enumRemap[aliasAttribute.Name] = member.GetValue(null);
                        }
                    }
                }
            }
        }
Exemple #21
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DictionaryDescriptor" /> class.
        /// </summary>
        /// <param name="attributeRegistry">The attribute registry.</param>
        /// <param name="type">The type.</param>
        /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param>
        /// <param name="respectPrivateSetters">If set to <c>true</c> will de/serialize properties with private setters.</param>
        /// <param name="namingConvention">The naming convention.</param>
        /// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.IDictionary;type</exception>
        public DictionaryDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, bool respectPrivateSetters, IMemberNamingConvention namingConvention)
            : base(attributeRegistry, type, emitDefaultValues, respectPrivateSetters, namingConvention)
        {
            if (!IsDictionary(type))
            {
                throw new ArgumentException("Expecting a type inheriting from System.Collections.IDictionary", "type");
            }

            // extract Key, Value types from IDictionary<??, ??>
            var interfaceType = type.GetInterface(typeof(IDictionary <,>));

            if (interfaceType != null)
            {
                keyType              = interfaceType.GetGenericArguments()[0];
                valueType            = interfaceType.GetGenericArguments()[1];
                IsGenericDictionary  = true;
                getEnumeratorGeneric = typeof(DictionaryDescriptor).GetMethod("GetGenericEnumerable").MakeGenericMethod(keyType, valueType);
                addMethod            = interfaceType.GetMethod("Add", new[] { keyType, valueType });
            }
            else
            {
                keyType   = typeof(object);
                valueType = typeof(object);
                addMethod = type.GetMethod("Add", new[] { keyType, valueType });
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CollectionDescriptor" /> class.
        /// </summary>
        /// <param name="factory">The factory.</param>
        /// <param name="type">The type.</param>
        /// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.ICollection;type</exception>
        public OldCollectionDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
            : base(factory, type, emitDefaultValues, namingConvention)
        {
            // Gets the element type
            ElementType = type.GetInterface(typeof(IEnumerable <>))?.GetGenericArguments()[0] ?? typeof(object);

            // implements IList
            if (typeof(IList).IsAssignableFrom(type))
            {
                CollectionAddFunction      = (obj, value) => ((IList)obj).Add(value);
                CollectionClearFunction    = obj => ((IList)obj).Clear();
                CollectionInsertFunction   = (obj, index, value) => ((IList)obj).Insert(index, value);
                CollectionRemoveAtFunction = (obj, index) => ((IList)obj).RemoveAt(index);
                GetCollectionCountFunction = o => ((IList)o).Count;
                GetIndexedItem             = (obj, index) => ((IList)obj)[index];
                SetIndexedItem             = (obj, index, value) => ((IList)obj)[index] = value;
                IsReadOnlyFunction         = obj => ((IList)obj).IsReadOnly;
                HasIndexerAccessors        = true;
                IsList = true;
            }
            else if (type.GetInterface(typeof(ICollection <>)) is Type itype)// implements ICollection<T>
            {
                var add = itype.GetMethod(nameof(ICollection <object> .Add), new[] { ElementType });
                CollectionAddFunction = (obj, value) => add.Invoke(obj, new[] { value });
                var remove = itype.GetMethod(nameof(ICollection <object> .Remove), new[] { ElementType });
                CollectionRemoveFunction = (obj, value) => remove.Invoke(obj, new[] { value });
                if (typeof(IDictionary).IsAssignableFrom(type))
                {
                    CollectionClearFunction    = obj => ((IDictionary)obj).Clear();
                    GetCollectionCountFunction = o => ((IDictionary)o).Count;
                    IsReadOnlyFunction         = obj => ((IDictionary)obj).IsReadOnly;
                }
                else
                {
                    var clear = itype.GetMethod(nameof(ICollection <object> .Clear), Type.EmptyTypes);
                    CollectionClearFunction = obj => clear.Invoke(obj, EmptyObjects);
                    var countMethod = itype.GetProperty(nameof(ICollection <object> .Count)).GetGetMethod();
                    GetCollectionCountFunction = o => (int)countMethod.Invoke(o, null);
                    var isReadOnly = itype.GetProperty(nameof(ICollection <object> .IsReadOnly)).GetGetMethod();
                    IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null);
                }
                // implements IList<T>
                itype = type.GetInterface(typeof(IList <>));
                if (itype != null)
                {
                    var insert = itype.GetMethod(nameof(IList <object> .Insert), new[] { typeof(int), ElementType });
                    CollectionInsertFunction = (obj, index, value) => insert.Invoke(obj, new[] { index, value });
                    var removeAt = itype.GetMethod(nameof(IList <object> .RemoveAt), new[] { typeof(int) });
                    CollectionRemoveAtFunction = (obj, index) => removeAt.Invoke(obj, new object[] { index });
                    var getItem = itype.GetMethod("get_Item", new[] { typeof(int) });
                    GetIndexedItem = (obj, index) => getItem.Invoke(obj, new object[] { index });
                    var setItem = itype.GetMethod("set_Item", new[] { typeof(int), ElementType });
                    SetIndexedItem      = (obj, index, value) => setItem.Invoke(obj, new[] { index, value });
                    HasIndexerAccessors = true;
                    IsList = true;
                }
                else
                {
                    // Attempt to retrieve IList<> accessors from ICollection.
                    var insert = type.GetMethod(nameof(IList <object> .Insert), new[] { typeof(int), ElementType });
                    if (insert != null)
                    {
                        CollectionInsertFunction = (obj, index, value) => insert.Invoke(obj, new[] { index, value });
                    }

                    var removeAt = type.GetMethod(nameof(IList <object> .RemoveAt), new[] { typeof(int) });
                    if (removeAt != null)
                    {
                        CollectionRemoveAtFunction = (obj, index) => removeAt.Invoke(obj, new object[] { index });
                    }

                    var getItem = type.GetMethod("get_Item", new[] { typeof(int) });
                    if (getItem != null)
                    {
                        GetIndexedItem = (obj, index) => getItem.Invoke(obj, new object[] { index });
                    }

                    var setItem = type.GetMethod("set_Item", new[] { typeof(int), ElementType });
                    if (setItem != null)
                    {
                        SetIndexedItem = (obj, index, value) => setItem.Invoke(obj, new[] { index, value });
                    }

                    HasIndexerAccessors = getItem != null && setItem != null;
                }
            }
            else
            {
                throw new ArgumentException($"Type [{(type)}] is not supported as a modifiable collection");
            }

            HasAdd      = CollectionAddFunction != null;
            HasRemove   = CollectionRemoveFunction != null;
            HasInsert   = CollectionInsertFunction != null;
            HasRemoveAt = CollectionRemoveAtFunction != null;
        }
 public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues, IMemberNamingConvention namingConvention)
     : this(attributeRegistry, emitDefaultValues, namingConvention, new DefaultKeyComparer())
 {
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        /// <param name="attributeRegistry">The attribute registry.</param>
        /// <param name="type">The type.</param>
        /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param>
        /// <param name="namingConvention">The naming convention.</param>
        /// <exception cref="System.ArgumentNullException">type</exception>
        /// <exception cref="YamlException">type</exception>
        public ObjectDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
        {
            if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry");
            if (type == null) throw new ArgumentNullException("type");
            if (namingConvention == null) throw new ArgumentNullException("namingConvention");

            this.memberNamingConvention = namingConvention;
            this.emitDefaultValues = emitDefaultValues;
            this.AttributeRegistry = attributeRegistry;
            this.type = type;
            var styleAttribute = AttributeRegistry.GetAttribute<YamlStyleAttribute>(type);
            this.style = styleAttribute != null ? styleAttribute.Style : YamlStyle.Any;
            this.IsCompilerGenerated = AttributeRegistry.GetAttribute<CompilerGeneratedAttribute>(type) != null;
        }
Exemple #25
0
        public DictionaryDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
            : base(factory, type, emitDefaultValues, namingConvention)
        {
            if (!IsDictionary(type))
            {
                throw new ArgumentException(@"Expecting a type inheriting from System.Collections.IDictionary", nameof(type));
            }

            // extract Key, Value types from IDictionary<??, ??>
            var interfaceType = type.GetInterface(typeof(IDictionary <,>));

            if (interfaceType != null)
            {
                KeyType              = interfaceType.GetGenericArguments()[0];
                ValueType            = interfaceType.GetGenericArguments()[1];
                IsGenericDictionary  = true;
                getEnumeratorGeneric = typeof(DictionaryDescriptor).GetMethod("GetGenericEnumerable").MakeGenericMethod(KeyType, ValueType);
                containsKeyMethod    = interfaceType.GetMethod("ContainsKey", new[] { KeyType });
                // Retrieve the other properties and methods from the interface
                type = interfaceType;
            }
            else
            {
                KeyType           = typeof(object);
                ValueType         = typeof(object);
                containsKeyMethod = type.GetMethod("Contains", new[] { KeyType });
            }

            addMethod       = type.GetMethod("Add", new[] { KeyType, ValueType });
            getKeysMethod   = type.GetProperty("Keys");
            getValuesMethod = type.GetProperty("Values");
            indexerProperty = type.GetProperty("Item", ValueType, new[] { KeyType });
            indexerSetter   = indexerProperty.SetMethod;
            removeMethod    = type.GetMethod("Remove", new[] { KeyType });
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
 /// </summary>
 /// <param name="attributeRegistry">The attribute registry.</param>
 /// <param name="type">The type.</param>
 /// <param name="namingConvention">The naming convention.</param>
 /// <exception cref="System.ArgumentException">Expecting arrat type;type</exception>
 public ArrayDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention)
     : this(attributeRegistry, type, false, namingConvention)
 {
 }
Exemple #27
0
        public SetDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
            : base(factory, type, emitDefaultValues, namingConvention)
        {
            if (!IsSet(type))
            {
                throw new ArgumentException(@"Expecting a type inheriting from System.Collections.ISet", nameof(type));
            }

            // extract Key, Value types from ISet<??>
            var interfaceType = type.GetInterface(typeof(ISet <>));

            // Gets the element type
            ElementType = interfaceType.GetGenericArguments()[0] ?? typeof(object);

            Type[] ArgTypes = { ElementType };
            addMethod        = interfaceType.GetMethod("Add", ArgTypes);
            removeMethod     = type.GetMethod("Remove", ArgTypes);
            clearMethod      = interfaceType.GetMethod("Clear");
            containsMethod   = type.GetMethod("Contains", ArgTypes);
            countMethod      = type.GetProperty("Count").GetGetMethod();
            isReadOnlyMethod = type.GetInterface(typeof(ICollection <>)).GetProperty("IsReadOnly").GetGetMethod();

            HasAdd              = true;
            HasRemove           = true;
            HasIndexerAccessors = true;
            HasInsert           = false;
            HasRemoveAt         = false;
        }
 public CollectionDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
     : base(factory, type, emitDefaultValues, namingConvention)
 {
 }
Exemple #29
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        /// <param name="factory">The factory.</param>
        /// <param name="type">The type.</param>
        /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception>
        public NullableDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
            : base(factory, type, emitDefaultValues, namingConvention)
        {
            if (!IsNullable(type))
            {
                throw new ArgumentException("Type [{0}] is not a primitive");
            }

            UnderlyingType = Nullable.GetUnderlyingType(type);
        }
Exemple #30
0
 public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues, IMemberNamingConvention namingConvention)
     : this(attributeRegistry, emitDefaultValues, namingConvention, new DefaultMemberComparer())
 {
 }
Exemple #31
0
        public ArrayDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
            : base(factory, type, emitDefaultValues, namingConvention)
        {
            if (!type.IsArray)
            {
                throw new ArgumentException(@"Expecting array type", nameof(type));
            }

            if (type.GetArrayRank() != 1)
            {
                throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".ToFormat(type.GetArrayRank(), type.FullName));
            }

            ElementType = type.GetElementType();
        }
Exemple #32
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CollectionDescriptor" /> class.
        /// </summary>
        /// <param name="attributeRegistry">The attribute registry.</param>
        /// <param name="type">The type.</param>
        /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param>
        /// <param name="namingConvention">The naming convention.</param>
        /// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.ICollection;type</exception>
        public CollectionDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, bool respectPrivateSetters, IMemberNamingConvention namingConvention)
            : base(attributeRegistry, type, emitDefaultValues, respectPrivateSetters, namingConvention)
        {
            if (!IsCollection(type))
            {
                throw new ArgumentException("Expecting a type inheriting from System.Collections.ICollection", "type");
            }

            // Gets the element type
            var collectionType = type.GetInterface(typeof(IEnumerable <>));

            ElementType = (collectionType != null) ? collectionType.GetGenericArguments()[0] : typeof(object);

            // implements ICollection<T>
            Type itype;

            if ((itype = type.GetInterface(typeof(ICollection <>))) != null)
            {
                var add = itype.GetMethod("Add", new[] { ElementType });
                CollectionAddFunction = (obj, value) => add.Invoke(obj, new[] { value });
                var countMethod = itype.GetProperty("Count").GetGetMethod();
                GetCollectionCountFunction = o => (int)countMethod.Invoke(o, null);
                var isReadOnly = itype.GetProperty("IsReadOnly").GetGetMethod();
                IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null);
                isKeyedCollection  = type.ExtendsGeneric(typeof(KeyedCollection <,>));
            }
            // implements IList
            else if (typeof(IList).IsAssignableFrom(type))
            {
                CollectionAddFunction      = (obj, value) => ((IList)obj).Add(value);
                GetCollectionCountFunction = o => ((IList)o).Count;
                IsReadOnlyFunction         = obj => ((IList)obj).IsReadOnly;
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CollectionDescriptor" /> class.
        /// </summary>
        /// <param name="factory">The factory.</param>
        /// <param name="type">The type.</param>
        /// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.ICollection;type</exception>
        public CollectionDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
            : base(factory, type, emitDefaultValues, namingConvention)
        {
            if (!IsCollection(type))
                throw new ArgumentException(@"Expecting a type inheriting from System.Collections.ICollection", nameof(type));

            // Gets the element type
            var collectionType = type.GetInterface(typeof(IEnumerable<>));
            ElementType = (collectionType != null) ? collectionType.GetGenericArguments()[0] : typeof(object);
            var typeSupported = false;

            // implements IList
            if (typeof(IList).IsAssignableFrom(type))
            {
                CollectionAddFunction = (obj, value) => ((IList)obj).Add(value);
                CollectionClearFunction = obj => ((IList)obj).Clear();
                CollectionInsertFunction = (obj, index, value) => ((IList)obj).Insert(index, value);
                CollectionRemoveAtFunction = (obj, index) => ((IList)obj).RemoveAt(index);
                GetCollectionCountFunction = o => ((IList)o).Count;
                GetIndexedItem = (obj, index) => ((IList)obj)[index];
                SetIndexedItem = (obj, index, value) => ((IList)obj)[index] = value;
                IsReadOnlyFunction = obj => ((IList)obj).IsReadOnly;
                HasIndexerAccessors = true;
                typeSupported = true;
                IsList = true;
            }
            var itype = type.GetInterface(typeof(ICollection<>));

            // implements ICollection<T> 
            if (!typeSupported && itype != null)
            {
                var remove = itype.GetMethod(nameof(ICollection<object>.Remove), new[] { ElementType });
                CollectionRemoveFunction = (obj, value) => remove.Invoke(obj, new[] { value });
                var add = itype.GetMethod(nameof(ICollection<object>.Add), new[] {ElementType});
                CollectionAddFunction = (obj, value) => add.Invoke(obj, new[] {value});
                var clear = itype.GetMethod(nameof(ICollection<object>.Clear), Type.EmptyTypes);
                CollectionClearFunction = obj => clear.Invoke(obj, EmptyObjects);
                var countMethod = itype.GetProperty(nameof(ICollection<object>.Count)).GetGetMethod();
                GetCollectionCountFunction = o => (int)countMethod.Invoke(o, null);
                var isReadOnly = itype.GetProperty(nameof(ICollection<object>.IsReadOnly)).GetGetMethod();
                IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null);
                typeSupported = true;
                // implements IList<T>
                itype = type.GetInterface(typeof(IList<>));
                if (itype != null)
                {
                    var insert = itype.GetMethod(nameof(IList<object>.Insert), new[] { typeof(int), ElementType });
                    CollectionInsertFunction = (obj, index, value) => insert.Invoke(obj, new[] { index, value });
                    var removeAt = itype.GetMethod(nameof(IList<object>.RemoveAt), new[] { typeof(int) });
                    CollectionRemoveAtFunction = (obj, index) => removeAt.Invoke(obj, new object[] { index });
                    var getItem = itype.GetMethod("get_Item", new[] { typeof(int) });
                    GetIndexedItem = (obj, index) => getItem.Invoke(obj, new object[] { index });
                    var setItem = itype.GetMethod("set_Item", new[] { typeof(int), ElementType });
                    SetIndexedItem = (obj, index, value) => setItem.Invoke(obj, new[] { index, value });
                    HasIndexerAccessors = true;
                    IsList = true;
                }
                else
                {
                    // Attempt to retrieve IList<> accessors from ICollection.
                    var insert = type.GetMethod(nameof(IList<object>.Insert), new[] { typeof(int), ElementType });
                    if (insert != null)
                        CollectionInsertFunction = (obj, index, value) => insert.Invoke(obj, new[] { index, value });

                    var removeAt = type.GetMethod(nameof(IList<object>.RemoveAt), new[] { typeof(int) });
                    if (removeAt != null)
                    CollectionRemoveAtFunction = (obj, index) => removeAt.Invoke(obj, new object[] { index });

                    var getItem = type.GetMethod("get_Item", new[] { typeof(int) });
                    if (getItem != null)
                        GetIndexedItem = (obj, index) => getItem.Invoke(obj, new object[] { index });

                    var setItem = type.GetMethod("set_Item", new[] { typeof(int), ElementType });
                    if (setItem != null)
                        SetIndexedItem = (obj, index, value) => setItem.Invoke(obj, new[] { index, value });

                    HasIndexerAccessors = getItem != null && setItem != null;
                }
            }

            if (!typeSupported)
            {
                throw new ArgumentException($"Type [{(type)}] is not supported as a modifiable collection");
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
 /// </summary>
 /// <param name="attributeRegistry">The attribute registry.</param>
 /// <param name="type">The type.</param>
 /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param>
 /// <param name="namingConvention">The naming convention.</param>
 /// <exception cref="System.ArgumentNullException">type</exception>
 /// <exception cref="YamlException">type</exception>
 public ObjectDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
     : this(attributeRegistry, type, emitDefaultValues, false, namingConvention)
 {
 }
Exemple #35
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NotSupportedObjectDescriptor" /> class.
 /// </summary>
 /// <param name="factory">The factory.</param>
 /// <param name="type">The type.</param>
 /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception>
 public NotSupportedObjectDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
     : base(factory, type, emitDefaultValues, namingConvention)
 {
 }
Exemple #36
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ListDescriptor" /> class.
        /// </summary>
        /// <param name="factory">The factory.</param>
        /// <param name="type">The type.</param>
        /// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.IList;type</exception>
        public ListDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
            : base(factory, type, emitDefaultValues, namingConvention)
        {
            if (!IsList(type))
            {
                throw new ArgumentException(@"Expecting a type inheriting from System.Collections.IList", nameof(type));
            }

            // Gets the element type
            ElementType = type.GetInterface(typeof(IEnumerable <>))?.GetGenericArguments()[0] ?? typeof(object);

            // implements IList
            if (typeof(IList).IsAssignableFrom(type))
            {
                // implements IList
                ListAddFunction      = (obj, value) => ((IList)obj).Add(value);
                ListClearFunction    = obj => ((IList)obj).Clear();
                ListInsertFunction   = (obj, index, value) => ((IList)obj).Insert(index, value);
                ListRemoveAtFunction = (obj, index) => ((IList)obj).RemoveAt(index);
                GetListCountFunction = o => ((IList)o).Count;
                GetIndexedItem       = (obj, index) => ((IList)obj)[index];
                SetIndexedItem       = (obj, index, value) => ((IList)obj)[index] = value;
                IsReadOnlyFunction   = obj => ((IList)obj).IsReadOnly;
            }
            else // implements IList<T>
            {
                var add = type.GetMethod(nameof(IList <object> .Add), new[] { ElementType });
                ListAddFunction = (obj, value) => add.Invoke(obj, new[] { value });
                var remove = type.GetMethod(nameof(IList <object> .Remove), new[] { ElementType });
                ListRemoveFunction = (obj, value) => remove.Invoke(obj, new[] { value });
                var clear = type.GetMethod(nameof(IList <object> .Clear), Type.EmptyTypes);
                ListClearFunction = obj => clear.Invoke(obj, EmptyObjects);
                var countMethod = type.GetProperty(nameof(IList <object> .Count)).GetGetMethod();
                GetListCountFunction = o => (int)countMethod.Invoke(o, null);
                var isReadOnly = type.GetInterface(typeof(ICollection <>)).GetProperty(nameof(IList <object> .IsReadOnly)).GetGetMethod();
                IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null);
                var insert = type.GetMethod(nameof(IList <object> .Insert), new[] { typeof(int), ElementType });
                ListInsertFunction = (obj, index, value) => insert.Invoke(obj, new[] { index, value });
                var removeAt = type.GetMethod(nameof(IList <object> .RemoveAt), new[] { typeof(int) });
                ListRemoveAtFunction = (obj, index) => removeAt.Invoke(obj, new object[] { index });
                var getItem = type.GetMethod("get_Item", new[] { typeof(int) });
                GetIndexedItem = (obj, index) => getItem.Invoke(obj, new object[] { index });
                var setItem = type.GetMethod("set_Item", new[] { typeof(int), ElementType });
                SetIndexedItem = (obj, index, value) => setItem.Invoke(obj, new[] { index, value });
            }

            HasAdd              = true;
            HasRemove           = true;
            HasInsert           = true;
            HasRemoveAt         = true;
            HasIndexerAccessors = true;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
 /// </summary>
 /// <param name="attributeRegistry">The attribute registry.</param>
 /// <param name="type">The type.</param>
 /// <param name="namingConvention">The naming convention.</param>
 /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception>
 public PrimitiveDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention)
     : base(attributeRegistry, type, false, namingConvention)
 {
     if (!IsPrimitive(type))
         throw new ArgumentException("Type [{0}] is not a primitive");
 }