/// <include file='doc\DocumentManager.uex' path='docs/doc[@for="DocumentManager.ComponentDesignerLoader.BeginLoad"]/*' />
            /// <devdoc>
            ///     The host will call this method when it wants to load whatever
            ///     data the code stream contains.  There are three times when this
            ///     method can be called.  First, a code stream is handed to the
            ///     host to initially create the document, so load will be called there.
            ///     Second, when a code steam is first declared through IPersistenceService,
            ///     it will be loaded immediately.  Finally, if the document needs to be
            ///     re-loaded because the user changed one or more files, then all code
            ///     streams will be re-loaded.
            /// </devdoc>
            public override void BeginLoad(IDesignerLoaderHost host)
            {
                if (this.host == null)
                {
                    host.AddService(typeof(IDesignerLoaderService), this);
                }

                this.host = host;

                loadCount = 1;
                if (errorList == null)
                {
                    errorList = new ArrayList();
                }
                else
                {
                    errorList.Clear();
                }

                bool successful = true;

                if (host != null)
                {
                    Type c = host.GetType(componentClass);
                    if (c == null)
                    {
                        errorList.Add(new Exception(SR.GetString(SR.PersisterClassNotFound, componentClass)));
                    }
                    else
                    {
                        IComponent component = null;

                        try {
                            component = host.CreateComponent(c);
                        }
                        catch (Exception e) {
                            errorList.Add(e);
                            successful = false;
                        }
                    }
                    ((IDesignerLoaderService)this).DependentLoadComplete(successful, errorList);
                }
            }
示例#2
0
        object IDesignerSerializationManager.GetSerializer(Type objectType, Type serializerType)
        {
            object serializer = null;

            if (objectType != null)
            {
                if (this._serializers != null)
                {
                    serializer = this._serializers[objectType];
                    if (!((serializer == null) || serializerType.IsAssignableFrom(serializer.GetType())))
                    {
                        serializer = null;
                    }
                }
                IDesignerLoaderHost host = this._loader.LoaderHost;
                if ((serializer == null) && (host != null))
                {
                    AttributeCollection attributes = TypeDescriptor.GetAttributes(objectType);
                    foreach (Attribute attr in attributes)
                    {
                        if (attr is DesignerSerializerAttribute)
                        {
                            DesignerSerializerAttribute da = (DesignerSerializerAttribute)attr;
                            string typeName = da.SerializerBaseTypeName;
                            if ((((typeName != null) && (host.GetType(typeName) == serializerType)) && (da.SerializerTypeName != null)) && (da.SerializerTypeName.Length > 0))
                            {
                                Type type = host.GetType(da.SerializerTypeName);
                                Debug.Assert(type != null, "Type " + objectType.FullName + " has a serializer that we couldn't bind to: " + da.SerializerTypeName);
                                if (type != null)
                                {
                                    serializer = Activator.CreateInstance(type, BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, null);
                                    break;
                                }
                            }
                        }
                    }
                    if (serializer != null)
                    {
                        if (this._serializers == null)
                        {
                            this._serializers = new Hashtable();
                        }
                        this._serializers[objectType] = serializer;
                    }
                }
            }
            if (this._designerSerializationProviders != null)
            {
                bool continueLoop = true;
                for (int i = 0; continueLoop && (i < this._designerSerializationProviders.Count); i++)
                {
                    continueLoop = false;
                    foreach (IDesignerSerializationProvider provider in this._designerSerializationProviders)
                    {
                        object newSerializer = provider.GetSerializer(this, serializer, objectType, serializerType);
                        if (newSerializer != null)
                        {
                            continueLoop = serializer != newSerializer;
                            serializer   = newSerializer;
                        }
                    }
                }
            }
            return(serializer);
        }
        ///     Retrieves a serializer of the requested type for the given
        ///     object type.
        object IDesignerSerializationManager.GetSerializer(Type objectType, Type serializerType)
        {
            object serializer = null;

            if (objectType != null)
            {
                if (_serializers != null)
                {
                    // I don't double hash here.  It will be a very rare day where
                    // multiple types of serializers will be used in the same scheme.
                    // We still handle it, but we just don't cache.
                    //
                    serializer = _serializers[objectType];
                    if (serializer != null && !serializerType.IsAssignableFrom(serializer.GetType()))
                    {
                        serializer = null;
                    }
                }

                // Now actually look in the type's metadata.
                //
                IDesignerLoaderHost host = _loader.LoaderHost;
                if (serializer == null && host != null)
                {
                    AttributeCollection attributes = TypeDescriptor.GetAttributes(objectType);
                    foreach (Attribute attr in attributes)
                    {
                        if (attr is DesignerSerializerAttribute)
                        {
                            DesignerSerializerAttribute da = (DesignerSerializerAttribute)attr;
                            string typeName = da.SerializerBaseTypeName;

                            // This serializer must support a CodeDomSerializer or we're not interested.
                            //
                            if (typeName != null && host.GetType(typeName) == serializerType && da.SerializerTypeName != null && da.SerializerTypeName.Length > 0)
                            {
                                Type type = host.GetType(da.SerializerTypeName);
                                Debug.Assert(type != null, "Type " + objectType.FullName + " has a serializer that we couldn't bind to: " + da.SerializerTypeName);
                                if (type != null)
                                {
                                    serializer = Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null);
                                    break;
                                }
                            }
                        }
                    }

                    // And stash this little guy for later.
                    //
                    if (serializer != null)
                    {
                        if (_serializers == null)
                        {
                            _serializers = new Hashtable();
                        }
                        _serializers[objectType] = serializer;
                    }
                }
            }

            // Designer serialization providers can override our metadata discovery.
            // We loop until we reach steady state.  This breaks order dependencies
            // by allowing all providers a chance to party on each other's serializers.
            //
            if (_designerSerializationProviders != null)
            {
                bool continueLoop = true;
                for (int i = 0; continueLoop && i < _designerSerializationProviders.Count; i++)
                {
                    continueLoop = false;

                    foreach (IDesignerSerializationProvider provider in _designerSerializationProviders)
                    {
                        object newSerializer = provider.GetSerializer(this, serializer, objectType, serializerType);
                        if (newSerializer != null)
                        {
                            continueLoop = (serializer != newSerializer);
                            serializer   = newSerializer;
                        }
                    }
                }
            }

            return(serializer);
        }