/// <summary>
 /// Initializes a new instance of the <see cref="NodeConstructingArgs"/> class.
 /// </summary>
 /// <param name="containerObjectDescriptor">The descriptor of the container of the member being constructed, or of the object itself it is a root object.</param>
 /// <param name="memberDescriptor">The member descriptor of the object being constructed if it is a member, or <c>null</c> otherwise.</param>
 public NodeConstructingArgs(ObjectDescriptor containerObjectDescriptor, MemberDescriptorBase memberDescriptor)
 {
     if (containerObjectDescriptor == null) throw new ArgumentNullException("containerObjectDescriptor");
     ContainerObjectDescriptor = containerObjectDescriptor;
     MemberDescriptor = memberDescriptor;
     ShouldProcessReference = true;
 }
        /// <inheritdoc/>
        public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
        {
            if (memberDescriptor != null)
            {
                var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute <MemberCollectionAttribute>(memberDescriptor.MemberInfo);
                if (attrib != null && attrib.ReadOnly)
                {
                    return(false);
                }
            }

            var dictionaryDescriptor = typeDescriptor as DictionaryDescriptor;

            if (dictionaryDescriptor == null)
            {
                return(false);
            }

            // Check key type
            if (!CanConstruct(dictionaryDescriptor.KeyType))
            {
                return(false);
            }

            // Check value type
            var elementType = dictionaryDescriptor.ValueType;

            return(CanConstruct(elementType) || elementType.IsAbstract || elementType.IsNullable() || IsReferenceType(elementType));
        }
Exemple #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NodeConstructingArgs"/> class.
 /// </summary>
 /// <param name="containerObjectDescriptor">The descriptor of the container of the member being constructed, or of the object itself it is a root object.</param>
 /// <param name="memberDescriptor">The member descriptor of the object being constructed if it is a member, or <c>null</c> otherwise.</param>
 public NodeConstructingArgs(ObjectDescriptor containerObjectDescriptor, MemberDescriptorBase memberDescriptor)
 {
     if (containerObjectDescriptor == null)
     {
         throw new ArgumentNullException("containerObjectDescriptor");
     }
     ContainerObjectDescriptor = containerObjectDescriptor;
     MemberDescriptor          = memberDescriptor;
 }
Exemple #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NodeConstructingArgs"/> class.
 /// </summary>
 /// <param name="containerObjectDescriptor">The descriptor of the container of the member being constructed, or of the object itself it is a root object.</param>
 /// <param name="memberDescriptor">The member descriptor of the object being constructed if it is a member, or <c>null</c> otherwise.</param>
 public NodeConstructingArgs(ObjectDescriptor containerObjectDescriptor, MemberDescriptorBase memberDescriptor)
 {
     if (containerObjectDescriptor == null)
     {
         throw new ArgumentNullException(nameof(containerObjectDescriptor));
     }
     ContainerObjectDescriptor = containerObjectDescriptor;
     MemberDescriptor          = memberDescriptor;
     ShouldProcessReference    = true;
 }
        /// <inheritdoc/>
        public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
        {
            var type = typeDescriptor.GetInnerCollectionType();
            var isNullableStruct = type.IsNullable() && Nullable.GetUnderlyingType(type).IsStruct();
            var isAbstractOrClass = type.IsAbstract || type.IsClass;
            //var isCollection = (typeDescriptor is CollectionDescriptor) || (typeDescriptor is DictionaryDescriptor);

            //var result = isNullableStruct || (isAbstractOrClass && !isCollection);
            var result = isNullableStruct || isAbstractOrClass;
            return result;
        }
Exemple #6
0
        private bool ShouldDiscardMember(MemberDescriptorBase memberDescriptor)
        {
            if (memberDescriptor == null || !DiscardUnbrowsable)
            {
                return(false);
            }

            var displayAttribute = TypeDescriptorFactory.AttributeRegistry.GetAttribute <DisplayAttribute>(memberDescriptor.MemberInfo);

            return(displayAttribute != null && !displayAttribute.Browsable);
        }
Exemple #7
0
        /// <inheritdoc/>
        public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
        {
            var type              = typeDescriptor.GetInnerCollectionType();
            var isNullableStruct  = type.IsNullable() && Nullable.GetUnderlyingType(type).IsStruct();
            var isAbstractOrClass = type.IsAbstract || type.IsClass;
            //var isCollection = (typeDescriptor is CollectionDescriptor) || (typeDescriptor is DictionaryDescriptor);

            //var result = isNullableStruct || (isAbstractOrClass && !isCollection);
            var result = isNullableStruct || isAbstractOrClass;

            return(result);
        }
Exemple #8
0
        /// <inheritdoc/>
        public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
        {
            if (memberDescriptor != null)
            {
                var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute<MemberCollectionAttribute>(memberDescriptor.MemberInfo);
                if (attrib != null && attrib.ReadOnly)
                    return false;
            }

            var collectionDescriptor = typeDescriptor as CollectionDescriptor;
            return collectionDescriptor != null && collectionDescriptor.HasInsert;
        }
        /// <inheritdoc/>
        public override bool CanAttach(ITypeDescriptor descriptor, MemberDescriptorBase memberDescriptor)
        {
            if (memberDescriptor != null)
            {
                var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute<MemberCollectionAttribute>(memberDescriptor.MemberInfo);
                if (attrib != null && attrib.ReadOnly)
                    return false;
            }

            var dictionaryDescriptor = descriptor as DictionaryDescriptor;
            return dictionaryDescriptor != null && dictionaryDescriptor.KeyType == typeof(string);
        }
 /// <inheritdoc/>
 public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
 {
     if (memberDescriptor != null)
     {
         var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute<MemberCollectionAttribute>(memberDescriptor.MemberInfo);
         if (attrib != null && attrib.ReadOnly)
             return false;
     }
     
     var dictionaryDescriptor = typeDescriptor as DictionaryDescriptor;
     if (dictionaryDescriptor == null)
         return false;
     return !dictionaryDescriptor.KeyType.IsClass || dictionaryDescriptor.KeyType == typeof(string) || dictionaryDescriptor.KeyType.GetConstructor(new Type[0]) != null;
 }
Exemple #11
0
        /// <inheritdoc/>
        public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
        {
            if (memberDescriptor != null)
            {
                var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute <MemberCollectionAttribute>(memberDescriptor.MemberInfo);
                if (attrib != null && attrib.ReadOnly)
                {
                    return(false);
                }
            }

            var collectionDescriptor = typeDescriptor as CollectionDescriptor;

            return(collectionDescriptor != null && collectionDescriptor.HasInsert);
        }
        /// <inheritdoc/>
        public override bool CanAttach(ITypeDescriptor descriptor, MemberDescriptorBase memberDescriptor)
        {
            if (memberDescriptor != null)
            {
                var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute <MemberCollectionAttribute>(memberDescriptor.MemberInfo);
                if (attrib != null && attrib.ReadOnly)
                {
                    return(false);
                }
            }

            var dictionaryDescriptor = descriptor as DictionaryDescriptor;

            return(dictionaryDescriptor != null && dictionaryDescriptor.KeyType == typeof(string));
        }
        /// <inheritdoc/>
        public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
        {
            if (memberDescriptor != null)
            {
                var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute<MemberCollectionAttribute>(memberDescriptor.MemberInfo);
                if (attrib?.ReadOnly == true)
                    return false;
            }
            
            var collectionDescriptor = typeDescriptor as CollectionDescriptor;
            if (collectionDescriptor == null)
                return false;

            var elementType = collectionDescriptor.ElementType;
            return collectionDescriptor.HasAdd && (CanConstruct(elementType) || CanAddNull(elementType) || IsReferenceType(elementType));
        }
        /// <inheritdoc/>
        public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
        {
            if (memberDescriptor != null)
            {
                var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute<MemberCollectionAttribute>(memberDescriptor.MemberInfo);
                if (attrib != null && attrib.ReadOnly)
                    return false;
            }
            
            var collectionDescriptor = typeDescriptor as CollectionDescriptor;
            if (collectionDescriptor == null)
                return false;

            var elementType = collectionDescriptor.ElementType;
            return collectionDescriptor.HasAdd && (!elementType.IsClass || elementType.GetConstructor(Type.EmptyTypes) != null || elementType.IsAbstract || elementType.IsNullable() || elementType == typeof(string));
        }
Exemple #15
0
        /// <inheritdoc/>
        public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
        {
            if (memberDescriptor != null)
            {
                var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute <MemberCollectionAttribute>(memberDescriptor.MemberInfo);
                if (attrib != null && attrib.ReadOnly)
                {
                    return(false);
                }
            }

            var dictionaryDescriptor = typeDescriptor as DictionaryDescriptor;

            if (dictionaryDescriptor == null)
            {
                return(false);
            }
            return(!dictionaryDescriptor.KeyType.IsClass || dictionaryDescriptor.KeyType == typeof(string) || dictionaryDescriptor.KeyType.GetConstructor(new Type[0]) != null);
        }
 /// <inheritdoc/>
 public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
 {
     if (memberDescriptor != null)
     {
         var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute<MemberCollectionAttribute>(memberDescriptor.MemberInfo);
         if (attrib != null && attrib.ReadOnly)
             return false;
     }
     
     var collectionDescriptor = typeDescriptor as CollectionDescriptor;
     var dictionaryDescriptor = typeDescriptor as DictionaryDescriptor;
     if (collectionDescriptor != null)
     {
         var elementType = collectionDescriptor.ElementType;
         // We also add the same conditions that for AddNewItem
         return collectionDescriptor.HasRemoveAt && (!elementType.IsClass || elementType.GetConstructor(Type.EmptyTypes) != null || elementType.IsAbstract || elementType.IsNullable() || elementType == typeof(string));
     }
     // TODO: add a HasRemove in the dictionary descriptor and test it!
     return dictionaryDescriptor != null;
 }
Exemple #17
0
        /// <inheritdoc/>
        public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
        {
            if (memberDescriptor != null)
            {
                var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute <MemberCollectionAttribute>(memberDescriptor.MemberInfo);
                if (attrib?.ReadOnly == true)
                {
                    return(false);
                }
            }

            var collectionDescriptor = typeDescriptor as CollectionDescriptor;

            if (collectionDescriptor == null)
            {
                return(false);
            }

            var elementType = collectionDescriptor.ElementType;

            return(collectionDescriptor.HasAdd && (CanConstruct(elementType) || elementType.IsAbstract || elementType.IsNullable() || IsReferenceType(elementType)));
        }
Exemple #18
0
        /// <inheritdoc/>
        public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
        {
            if (memberDescriptor != null)
            {
                var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute <MemberCollectionAttribute>(memberDescriptor.MemberInfo);
                if (attrib != null && attrib.ReadOnly)
                {
                    return(false);
                }
            }

            var collectionDescriptor = typeDescriptor as CollectionDescriptor;

            if (collectionDescriptor == null)
            {
                return(false);
            }

            var elementType = collectionDescriptor.ElementType;

            return(collectionDescriptor.HasAdd && (!elementType.IsClass || elementType.GetConstructor(Type.EmptyTypes) != null || elementType.IsAbstract || elementType.IsNullable() || elementType == typeof(string)));
        }
Exemple #19
0
        /// <inheritdoc/>
        public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
        {
            if (memberDescriptor != null)
            {
                var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute <MemberCollectionAttribute>(memberDescriptor.MemberInfo);
                if (attrib != null && attrib.ReadOnly)
                {
                    return(false);
                }
            }

            var collectionDescriptor = typeDescriptor as CollectionDescriptor;
            var dictionaryDescriptor = typeDescriptor as DictionaryDescriptor;

            if (collectionDescriptor != null)
            {
                var elementType = collectionDescriptor.ElementType;
                // We also add the same conditions that for AddNewItem
                return(collectionDescriptor.HasRemoveAt && (!elementType.IsClass || elementType.GetConstructor(Type.EmptyTypes) != null || elementType.IsAbstract || elementType.IsNullable() || elementType == typeof(string)));
            }
            // TODO: add a HasRemove in the dictionary descriptor and test it!
            return(dictionaryDescriptor != null);
        }
        protected override bool PrepareMember(MemberDescriptorBase member)
        {
            // Filter members
            if (member is PropertyDescriptor && ListOfMembersToRemove.Contains(member.Name))
            //if (member is PropertyDescriptor && (member.DeclaringType.Namespace ?? string.Empty).StartsWith(SystemCollectionsNamespace) && ListOfMembersToRemove.Contains(member.Name))
            {
                return false;
            }

            return !IsCompilerGenerated && base.PrepareMember(member);
        }
Exemple #21
0
        /// <inheritdoc/>
        public bool CanAttach(ITypeDescriptor descriptor, MemberDescriptorBase memberDescriptor)
        {
            var dictionaryDescriptor = descriptor as DictionaryDescriptor;

            return(dictionaryDescriptor != null && dictionaryDescriptor.KeyType == typeof(string));
        }
Exemple #22
0
        protected virtual bool PrepareMember(MemberDescriptorBase member)
        {
            var memberType = member.Type;

            // If the member has a set, this is a conventional assign method
            if (member.HasSet)
            {
                member.Mode = DataMemberMode.Assign;
            }
            else
            {
                // Else we cannot only assign its content if it is a class
                member.Mode = (memberType != typeof(string) && memberType.IsClass) || memberType.IsInterface || type.IsAnonymous() ? DataMemberMode.Content : DataMemberMode.Never;
            }

            // Gets the style
            var styleAttribute = AttributeRegistry.GetAttribute<DataStyleAttribute>(member.MemberInfo);
            member.Style = styleAttribute != null ? styleAttribute.Style : DataStyle.Any;

            // Handle member attribute
            var memberAttribute = AttributeRegistry.GetAttribute<DataMemberAttribute>(member.MemberInfo);
            if (memberAttribute != null)
            {
                if (!member.HasSet)
                {
                    if (memberAttribute.Mode == DataMemberMode.Assign ||
                        (memberType.IsValueType && member.Mode == DataMemberMode.Content))
                        throw new ArgumentException("{0} {1} is not writeable by {2}.".ToFormat(memberType.FullName, member.Name, memberAttribute.Mode.ToString()));
                }

                if (memberAttribute.Mode != DataMemberMode.Default)
                {
                    member.Mode = memberAttribute.Mode;
                }
                member.Order = memberAttribute.Order;
            }

            if (member.Mode == DataMemberMode.Binary)
            {
                if (!memberType.IsArray)
                    throw new InvalidOperationException("{0} {1} of {2} is not an array. Can not be serialized as binary."
                                                            .ToFormat(memberType.FullName, member.Name, type.FullName));
                if (!memberType.GetElementType().IsPureValueType())
                    throw new InvalidOperationException("{0} is not a pure ValueType. {1} {2} of {3} can not serialize as binary.".ToFormat(memberType.GetElementType(), memberType.FullName, member.Name, type.FullName));
            }

            // If this member cannot be serialized, remove it from the list
            if (member.Mode == DataMemberMode.Never)
            {
                return false;
            }

            if (memberAttribute != null && !string.IsNullOrEmpty(memberAttribute.Name))
            {
                member.Name = memberAttribute.Name;
            }

            return true;
        }
Exemple #23
0
 /// <inheritdoc/>
 public abstract bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor);
 /// <summary>
 /// Initializes a new instance of the <see cref="NodeConstructingArgs"/> class.
 /// </summary>
 /// <param name="containerObjectDescriptor">The descriptor of the container of the member being constructed, or of the object itself it is a root object.</param>
 /// <param name="memberDescriptor">The member descriptor of the object being constructed if it is a member, or <c>null</c> otherwise.</param>
 public NodeConstructingArgs(ObjectDescriptor containerObjectDescriptor, MemberDescriptorBase memberDescriptor)
 {
     if (containerObjectDescriptor == null) throw new ArgumentNullException(nameof(containerObjectDescriptor));
     ContainerObjectDescriptor = containerObjectDescriptor;
     MemberDescriptor = memberDescriptor;
 }
 /// <inheritdoc/>
 public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
 {
     return((typeDescriptor.Type.IsNullable() && Nullable.GetUnderlyingType(typeDescriptor.Type).IsStruct()) ||
            (typeDescriptor.Type.IsAbstract && !(typeDescriptor is CollectionDescriptor) && !(typeDescriptor is DictionaryDescriptor)));
 }
Exemple #26
0
 /// <inheritdoc/>
 public abstract bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor);
 /// <inheritdoc/>
 public override bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
 {
     return(((typeDescriptor.Type.IsNullable() && typeDescriptor.Type.IsStruct()) || typeDescriptor.Type.IsClass || typeDescriptor.Type.IsAbstract) &&
            !(typeDescriptor is CollectionDescriptor) && !(typeDescriptor is DictionaryDescriptor));
 }
        protected virtual bool PrepareMember(MemberDescriptorBase member)
        {
            var memberType = member.Type;

            // The default mode is Content, which will not use the setter to restore value if the object is a class (but behave like Assign for value types)
            member.Mode = DataMemberMode.Content;
            if (!member.HasSet && (memberType == typeof(string) || !memberType.IsClass) && !memberType.IsInterface && !Type.IsAnonymous())
            {
                // If there is no setter, and the value is a string or a value type, we won't write the object at all.
                member.Mode = DataMemberMode.Never;
            }

            // Gets the style
            var styleAttribute = AttributeRegistry.GetAttribute<DataStyleAttribute>(member.MemberInfo);
            member.Style = styleAttribute?.Style ?? DataStyle.Any;
            member.Mask = 1;

            // Handle member attribute
            var memberAttribute = AttributeRegistry.GetAttribute<DataMemberAttribute>(member.MemberInfo);
            if (memberAttribute != null)
            {
                ((IMemberDescriptor)member).Mask = memberAttribute.Mask;
                if (!member.HasSet)
                {
                    if (memberAttribute.Mode == DataMemberMode.Assign ||
                        (memberType.IsValueType && member.Mode == DataMemberMode.Content))
                        throw new ArgumentException($"{memberType.FullName} {member.OriginalName} is not writeable by {memberAttribute.Mode.ToString()}.");
                }

                if (memberAttribute.Mode != DataMemberMode.Default)
                {
                    member.Mode = memberAttribute.Mode;
                }
                member.Order = memberAttribute.Order;
            }

            // Process all attributes just once instead of getting them one by one
            var attributes = AttributeRegistry.GetAttributes(member.MemberInfo);
            DefaultValueAttribute defaultValueAttribute = null;
            foreach (var attribute in attributes)
            {
                var valueAttribute = attribute as DefaultValueAttribute;
                if (valueAttribute != null)
                {
                    defaultValueAttribute = valueAttribute;
                    continue;
                }

                var yamlRemap = attribute as DataAliasAttribute;
                if (yamlRemap != null)
                {
                    if (member.AlternativeNames == null)
                    {
                        member.AlternativeNames = new List<string>();
                    }
                    if (!string.IsNullOrWhiteSpace(yamlRemap.Name))
                    {
                        member.AlternativeNames.Add(yamlRemap.Name);
                    }
                }
            }


            // If it's a private member, check it has a YamlMemberAttribute on it
            if (!member.IsPublic)
            {
                if (memberAttribute == null)
                    return false;
            }

            if (member.Mode == DataMemberMode.Binary)
            {
                if (!memberType.IsArray)
                    throw new InvalidOperationException($"{memberType.FullName} {member.OriginalName} of {Type.FullName} is not an array. Can not be serialized as binary.");
                if (!memberType.GetElementType().IsPureValueType())
                    throw new InvalidOperationException($"{memberType.GetElementType()} is not a pure ValueType. {memberType.FullName} {member.OriginalName} of {Type.FullName} can not serialize as binary.");
            }

            // If this member cannot be serialized, remove it from the list
            if (member.Mode == DataMemberMode.Never)
            {
                return false;
            }

            // ShouldSerialize
            //	  YamlSerializeAttribute(Never) => false
            //	  ShouldSerializeSomeProperty => call it
            //	  DefaultValueAttribute(default) => compare to it
            //	  otherwise => true
            var shouldSerialize = Type.GetMethod("ShouldSerialize" + member.OriginalName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            if (shouldSerialize != null && shouldSerialize.ReturnType == typeof(bool) && member.ShouldSerialize == null)
                member.ShouldSerialize = obj => (bool)shouldSerialize.Invoke(obj, EmptyObjectArray);

            if (defaultValueAttribute != null && member.ShouldSerialize == null && !emitDefaultValues)
            {
                object defaultValue = defaultValueAttribute.Value;
                Type defaultType = defaultValue?.GetType();
                if (defaultType.IsNumeric() && defaultType != memberType)
                    defaultValue = memberType.CastToNumericType(defaultValue);
                member.ShouldSerialize = obj => !Equals(defaultValue, member.Get(obj));
            }

            if (member.ShouldSerialize == null)
                member.ShouldSerialize = ShouldSerializeDefault;

            member.Name = !string.IsNullOrEmpty(memberAttribute?.Name) ? memberAttribute.Name : NamingConvention.Convert(member.OriginalName);

            return true;
        }