/// <summary>
        /// Initializes a new instance of the <see cref="EntityCollectionScanTarget"/> class.
        /// </summary>
        /// <param name="entityType">
        /// The entity type.
        /// </param>
        /// <param name="entitySpec">
        /// The entity specification.
        /// </param>
        /// <param name="inspectedEnumerable">
        /// The inspected enumerable.
        /// </param>
        /// <param name="collection">
        /// The target collection.
        /// </param>
        /// <exception cref="PersistenceException">
        /// If the <paramref name="inspectedEnumerable"/> does not have an add method.
        /// </exception>
        internal EntityCollectionScanTarget(Type entityType, EntitySpec entitySpec, InspectedEnumerable inspectedEnumerable, object collection)
            : base(entityType, entitySpec)
        {
            if (!inspectedEnumerable.HasAdd)
            {
                throw new PersistenceException(
                    string.Format(CultureInfo.InvariantCulture, @"Enumerable {0} has no void Add({1}) method.", inspectedEnumerable.InspectedType, inspectedEnumerable.ElementType));
            }

            this.setter = this.Add;
            this.add = inspectedEnumerable.Add;
            this.collection = collection;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="EntityIndexerScanTarget"/> class.
        /// </summary>
        /// <param name="entityType">
        /// The entity type.
        /// </param>
        /// <param name="entitySpec">
        /// The entity spec.
        /// </param>
        /// <param name="inspectedEnumerable">
        /// The inspected enumerable.
        /// </param>
        /// <param name="collection">
        /// The collection.
        /// </param>
        /// <param name="pos">
        /// The position in the indexed collection.
        /// </param>
        /// <exception cref="PersistenceException">
        /// If the <paramref name="inspectedEnumerable"/> does not have an indexer.
        /// </exception>
        internal EntityIndexerScanTarget(Type entityType, EntitySpec entitySpec, InspectedEnumerable inspectedEnumerable, object collection, int pos)
            : base(entityType, entitySpec)
        {
            if (!inspectedEnumerable.HasIndexer)
            {
                throw new PersistenceException(
                    string.Format(CultureInfo.InvariantCulture, @"Enumerable {0} has no index for type {1}.", inspectedEnumerable.InspectedType, inspectedEnumerable.ElementType));
            }

            this.setter = this.Set;
            this.inspectedEnumerable = inspectedEnumerable;
            this.collection = collection;
            this.pos = pos;
        }
Example #3
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.constructor = DelegateFactory.CreateConstructor(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);
            }
        }