Пример #1
0
        public object GetSerializer(Type objectType, Type serializerType)
        {
            if (serializerType == null)
            {
                throw new ArgumentNullException("serializerType");
            }
            object currentSerializer = null;

            if (objectType != null)
            {
                if (this.serializers != null)
                {
                    currentSerializer = this.serializers[objectType];
                    if ((currentSerializer != null) && !serializerType.IsAssignableFrom(currentSerializer.GetType()))
                    {
                        currentSerializer = null;
                    }
                }
                if (currentSerializer == null)
                {
                    foreach (Attribute attribute in TypeDescriptor.GetAttributes(objectType))
                    {
                        if (attribute is DesignerSerializerAttribute)
                        {
                            DesignerSerializerAttribute attribute2 = (DesignerSerializerAttribute)attribute;
                            string serializerBaseTypeName          = attribute2.SerializerBaseTypeName;
                            if (((serializerBaseTypeName != null) && (this.GetRuntimeType(serializerBaseTypeName) == serializerType)) && ((attribute2.SerializerTypeName != null) && (attribute2.SerializerTypeName.Length > 0)))
                            {
                                Type runtimeType = this.GetRuntimeType(attribute2.SerializerTypeName);
                                if (runtimeType != null)
                                {
                                    currentSerializer = Activator.CreateInstance(runtimeType, BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, null);
                                    break;
                                }
                            }
                        }
                    }
                    if ((currentSerializer != null) && (this.session != null))
                    {
                        if (this.serializers == null)
                        {
                            this.serializers = new Hashtable();
                        }
                        this.serializers[objectType] = currentSerializer;
                    }
                }
            }
            if ((this.defaultProviderTable == null) || !this.defaultProviderTable.ContainsKey(serializerType))
            {
                Type c = null;
                DefaultSerializationProviderAttribute attribute3 = (DefaultSerializationProviderAttribute)TypeDescriptor.GetAttributes(serializerType)[typeof(DefaultSerializationProviderAttribute)];
                if (attribute3 != null)
                {
                    c = this.GetRuntimeType(attribute3.ProviderTypeName);
                    if ((c != null) && typeof(IDesignerSerializationProvider).IsAssignableFrom(c))
                    {
                        IDesignerSerializationProvider provider = (IDesignerSerializationProvider)Activator.CreateInstance(c, BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, null);
                        ((IDesignerSerializationManager)this).AddSerializationProvider(provider);
                    }
                }
                if (this.defaultProviderTable == null)
                {
                    this.defaultProviderTable = new Hashtable();
                }
                this.defaultProviderTable[serializerType] = c;
            }
            if (this.designerSerializationProviders != null)
            {
                bool flag = true;
                for (int i = 0; flag && (i < this.designerSerializationProviders.Count); i++)
                {
                    flag = false;
                    foreach (IDesignerSerializationProvider provider2 in this.designerSerializationProviders)
                    {
                        object obj3 = provider2.GetSerializer(this, currentSerializer, objectType, serializerType);
                        if (obj3 != null)
                        {
                            flag = currentSerializer != obj3;
                            currentSerializer = obj3;
                        }
                    }
                }
            }
            return(currentSerializer);
        }
Пример #2
0
        public object GetSerializer(Type componentType, Type serializerType)
        {
            VerifyInSession();

            if (serializerType == null)
            {
                throw new ArgumentNullException("serializerType");
            }

            object serializer = null;

            if (componentType != null)
            {
                // try 1: from cache
                //
                _serializersCache.TryGetValue(componentType, out serializer);

                // check for provider attribute and add it to the list of providers
                //
                if (serializer != null && !serializerType.IsAssignableFrom(serializer.GetType()))
                {
                    serializer = null;
                }

                AttributeCollection attributes = TypeDescriptor.GetAttributes(componentType);
                DefaultSerializationProviderAttribute providerAttribute = attributes[typeof(DefaultSerializationProviderAttribute)]
                                                                          as DefaultSerializationProviderAttribute;
                if (providerAttribute != null && this.GetType(providerAttribute.ProviderTypeName) == serializerType)
                {
                    object provider = Activator.CreateInstance(this.GetType(providerAttribute.ProviderTypeName),
                                                               BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.NonPublic,
                                                               null, null, null);
                    ((IDesignerSerializationManager)this).AddSerializationProvider((IDesignerSerializationProvider)provider);
                }
            }

            // try 2: DesignerSerializerAttribute
            //
            if (serializer == null && componentType != null)
            {
                AttributeCollection         attributes          = TypeDescriptor.GetAttributes(componentType);
                DesignerSerializerAttribute serializerAttribute = attributes[typeof(DesignerSerializerAttribute)] as DesignerSerializerAttribute;
                if (serializerAttribute != null &&
                    this.GetType(serializerAttribute.SerializerBaseTypeName) == serializerType)
                {
                    try {
                        serializer = Activator.CreateInstance(this.GetType(serializerAttribute.SerializerTypeName),
                                                              BindingFlags.CreateInstance | BindingFlags.Instance |
                                                              BindingFlags.Public | BindingFlags.NonPublic,
                                                              null, null, null);
                    } catch {}
                }

                if (serializer != null)
                {
                    _serializersCache[componentType] = serializer;
                }
            }

            // try 3: from provider
            //
            if (serializer == null && _serializationProviders != null)
            {
                foreach (IDesignerSerializationProvider provider in _serializationProviders)
                {
                    serializer = provider.GetSerializer(this, null, componentType, serializerType);
                    if (serializer != null)
                    {
                        break;
                    }
                }
            }

            return(serializer);
        }
Пример #3
0
        /// <summary>Gets the serializer for the given object type.</summary>
        /// <returns>The serializer for <paramref name="objectType" />, or null, if not found.</returns>
        /// <param name="objectType">The type of object for which to retrieve the serializer.</param>
        /// <param name="serializerType">The type of serializer to retrieve.</param>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="objectType" /> or <paramref name="serializerType" /> is null.</exception>
        public object GetSerializer(Type objectType, Type serializerType)
        {
            if (serializerType == null)
            {
                throw new ArgumentNullException("serializerType");
            }
            object obj = null;

            if (objectType != null)
            {
                if (this.serializers != null)
                {
                    obj = this.serializers[objectType];
                    if (obj != null && !serializerType.IsAssignableFrom(obj.GetType()))
                    {
                        obj = null;
                    }
                }
                if (obj == null)
                {
                    AttributeCollection attributes = TypeDescriptor.GetAttributes(objectType);
                    foreach (Attribute attribute in attributes)
                    {
                        if (attribute is DesignerSerializerAttribute designerSerializerAttribute)
                        {
                            string serializerBaseTypeName = designerSerializerAttribute.SerializerBaseTypeName;
                            if (serializerBaseTypeName != null)
                            {
                                Type runtimeType = this.GetRuntimeType(serializerBaseTypeName);
                                if (runtimeType == serializerType && designerSerializerAttribute.SerializerTypeName != null && designerSerializerAttribute.SerializerTypeName.Length > 0)
                                {
                                    Type runtimeType2 = this.GetRuntimeType(designerSerializerAttribute.SerializerTypeName);
                                    if (runtimeType2 != null)
                                    {
                                        obj = Activator.CreateInstance(runtimeType2, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (obj != null && this.session != null)
                    {
                        if (this.serializers == null)
                        {
                            this.serializers = new Hashtable();
                        }
                        this.serializers[objectType] = obj;
                    }
                }
            }
            if (this.defaultProviderTable == null || !this.defaultProviderTable.ContainsKey(serializerType))
            {
                Type type = null;
                DefaultSerializationProviderAttribute defaultSerializationProviderAttribute = (DefaultSerializationProviderAttribute)TypeDescriptor.GetAttributes(serializerType)[typeof(DefaultSerializationProviderAttribute)];
                if (defaultSerializationProviderAttribute != null)
                {
                    type = this.GetRuntimeType(defaultSerializationProviderAttribute.ProviderTypeName);
                    if (type != null && typeof(IDesignerSerializationProvider).IsAssignableFrom(type))
                    {
                        IDesignerSerializationProvider designerSerializationProvider = (IDesignerSerializationProvider)Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null);
                        ((IDesignerSerializationManager)this).AddSerializationProvider(designerSerializationProvider);
                    }
                }
                if (this.defaultProviderTable == null)
                {
                    this.defaultProviderTable = new Hashtable();
                }
                this.defaultProviderTable[serializerType] = type;
            }
            if (this.designerSerializationProviders != null)
            {
                bool flag = true;
                int  num  = 0;
                while (flag && num < this.designerSerializationProviders.Count)
                {
                    flag = false;
                    foreach (IDesignerSerializationProvider designerSerializationProvider2 in this.designerSerializationProviders)
                    {
                        object serializer = designerSerializationProvider2.GetSerializer(this, obj, objectType, serializerType);
                        if (serializer != null)
                        {
                            flag = (obj != serializer);
                            obj  = serializer;
                        }
                    }
                    num++;
                }
            }
            return(obj);
        }