示例#1
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.GetSerializerType(serializerAttribute.SerializerBaseTypeName) == serializerType)
                {
                    try {
                        serializer = Activator.CreateInstance(this.GetSerializerType(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);
        }
示例#2
0
        public object GetSerializer(Type componentType, Type serializerType)
        {
            VerifyInSession();

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

            // try 1: from cache
            //
            object serializer = _serializersCache[componentType];

            if (!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: from provider
            if (serializer == null)
            {
                foreach (IDesignerSerializationProvider provider in _serializationProviders)
                {
                    serializer = provider.GetSerializer(this, _serializersCache[componentType], componentType, serializerType);
                    if (serializer != null)
                    {
                        break;
                    }
                }
            }

            // try 3: Activator
            if (serializer == null)
            {
                DesignerSerializerAttribute serializerAttribute = attributes[typeof(DesignerSerializerAttribute)] as DesignerSerializerAttribute;
                if (serializerAttribute != null && this.GetType(serializerAttribute.SerializerTypeName) == serializerType)
                {
                    serializer = Activator.CreateInstance(this.GetType(serializerAttribute.SerializerTypeName),
                                                          BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.NonPublic,
                                                          null, null, null);
                }
            }

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

            return(serializer);
        }