public override void Process(IMetaData metaData, ICustomAttributeProvider attributeProvider, IConfiguration config)
        {
            TypeData typeData = metaData as TypeData;
            if (typeData == null)
                return;

            JsonExCollectionAttribute attr = ReflectionUtils.GetAttribute<JsonExCollectionAttribute>(attributeProvider, true);
            if (attr == null)
                return;

            if (!attr.IsValid())
                throw new Exception("Invalid JsonExCollectionAttribute specified for " + attributeProvider + ", either CollectionHandlerType or ItemType or both must be specified");

            

            Type collHandlerType = attr.GetCollectionHandlerType();
            Type itemType = attr.GetItemType();

            // Try exact type match first
            CollectionHandler handler = null;

            if (collHandlerType == null)
            {
                handler = typeData.FindCollectionHandler();
                handler = new CollectionHandlerWrapper(handler, typeData.ForType, itemType);
            }

            bool registerHandler = false;
            if (handler == null)
            {
                handler = ConstructOrFindHandler(config, collHandlerType, ref registerHandler);
            }

            typeData.CollectionHandler = handler;
            // install the handler
            if (registerHandler)
                config.RegisterCollectionHandler(handler);
            
        }
Example #2
0
        /// <summary>
        /// Reads the JsonExCollection attribute for the class and loads the collection handler from that.  It first
        /// checks the list of collectionhandlers to see if its already been loaded.
        /// </summary>
        /// <returns>CollectionHandler specified by the JsonExCollection attribute</returns>
        protected virtual CollectionHandler GetCollectionHandlerFromAttribute()
        {
            JsonExCollectionAttribute attr = ((JsonExCollectionAttribute[])this.ForType.GetCustomAttributes(typeof(JsonExCollectionAttribute), true))[0];
            if (!attr.IsValid())
                throw new Exception("Invalid JsonExCollectionAttribute specified for " + this.ForType + ", either CollectionHandlerType or ItemType or both must be specified");

            Type collHandlerType = attr.GetCollectionHandlerType();
            Type itemType = attr.GetItemType();

            // Try exact type match first
            CollectionHandler handler = null;

            if (collHandlerType == null)
            {
                handler = FindCollectionHandler();
                handler = new CollectionHandlerWrapper(handler, this.ForType, itemType);
            }

            if (handler == null)
            {
                handler = context.CollectionHandlers.Find(delegate(CollectionHandler h) { return h.GetType() == collHandlerType; });
                if (handler != null)
                    return handler;

                // try inherited type next
                handler = context.CollectionHandlers.Find(delegate(CollectionHandler h) { return collHandlerType.IsInstanceOfType(h); });
                if (handler != null)
                    return handler;

                // create the handler
                handler = (CollectionHandler)Activator.CreateInstance(collHandlerType);
            }

            // install the handler
            context.RegisterCollectionHandler(handler);
            return handler;
        }