Пример #1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="Inspector" /> class.
        /// </summary>
        /// <param name="type">
        ///     The type to inspect.
        /// </param>
        private Inspector(Type type)
        {
            this.InspectedType = type;

            if (this.IsArray = type.IsArray)
            {
                return;
            }

            if (this.IsEnum = type.IsEnum)
            {
                this.EnumType = type.GetEnumUnderlyingType();
                return;
            }

            if (this.IsTuple = type.IsTuple())
            {
                return;
            }

            if (this.IsKeyValuePair = type.IsGenericTypeDefinition(typeof(KeyValuePair <,>)))
            {
                return;
            }

            if (!typeof(Enumerable).IsAssignableFrom(type))
            {
                this.createInstance = DelegateFactory.CreateInstance(type);

                ////TODO control isSerializable/ISerializable by settings fields or props?
                this.inspectedProperties = ReflectionExtensions.HasAttribute <SerializableAttribute>(type)
                    ? InspectFields(type)
                    : InspectProperties(type);
                this.inspectedPropertiesPositional = this.inspectedProperties.ToArray();

                if (typeof(IDictionary).IsAssignableFrom(type))
                {
                    ////TODO impl support for comparer (also for set's)
                }
                else if (typeof(IEnumerable).IsAssignableFrom(type))
                {
                    this.enumerable = InspectEnumerable(type);
                }
                else if (type.HasInterface(typeof(ISerializable)))
                {
                    this.serializable = InspectSerializable(type);
                    if (this.serializable != null)
                    {
                        this.inspectedProperties.Clear();
                    }
                }

                this.OnSerializing            = CreateHandler <OnSerializingAttribute>(type);
                this.OnSerialized             = CreateHandler <OnSerializedAttribute>(type);
                this.OnDeserializing          = CreateHandler <OnDeserializingAttribute>(type);
                this.OnDeserialized           = CreateHandler <OnDeserializedAttribute>(type);
                this.hasSerializationHandlers = this.OnSerializing != null || this.OnSerialized != null ||
                                                this.OnDeserializing != null || this.OnDeserialized != null;
            }
            else
            {
                this.enumerable = InspectEnumerable(type);
            }
        }