예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
        /// </summary>
        public ObjectDescriptor(ITypeDescriptorFactory factory, [NotNull] 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;
            }
        }
예제 #2
0
        protected bool IsMemberToVisit(MemberInfo memberInfo)
        {
            // Remove all SyncRoot from members
            if (memberInfo is PropertyInfo && memberInfo.Name == "SyncRoot" && memberInfo.DeclaringType != null && (memberInfo.DeclaringType.Namespace ?? string.Empty).StartsWith(SystemCollectionsNamespace))
            {
                return(false);
            }

            Type memberType = null;
            var  fieldInfo  = memberInfo as FieldInfo;

            if (fieldInfo != null)
            {
                memberType = fieldInfo.FieldType;
            }
            else
            {
                var propertyInfo = memberInfo as PropertyInfo;
                if (propertyInfo != null)
                {
                    memberType = propertyInfo.PropertyType;
                }
            }

            if (memberType != null)
            {
                if (typeof(Delegate).IsAssignableFrom(memberType))
                {
                    return(false);
                }
            }


            // Member is not displayed if there is a YamlIgnore attribute on it
            if (AttributeRegistry.GetAttribute <DataMemberIgnoreAttribute>(memberInfo) != null)
            {
                return(false);
            }

            return(true);
        }