Exemple #1
0
        public void Ctor_SerializerTypeName_BaseSerializerTypeName(string serializerTypeName, string baseSerializerTypeName)
        {
            var attribute = new DesignerSerializerAttribute(serializerTypeName, baseSerializerTypeName);

            Assert.Equal(serializerTypeName, attribute.SerializerTypeName);
            Assert.Equal(baseSerializerTypeName, attribute.SerializerBaseTypeName);
        }
Exemple #2
0
        public void TypeId_Get_ReturnsExcepted(string serializerBaseTypeName, object expected)
        {
            var attribute = new DesignerSerializerAttribute("SerializerType", serializerBaseTypeName);

            Assert.Equal(expected, attribute.TypeId);
            Assert.Same(attribute.TypeId, attribute.TypeId);
        }
Exemple #3
0
        public void Ctor_SerializerType_BaseSerializerType(Type serializerType, Type baseSerializerType)
        {
            var attribute = new DesignerSerializerAttribute(serializerType, baseSerializerType);

            Assert.Equal(serializerType.AssemblyQualifiedName, attribute.SerializerTypeName);
            Assert.Equal(baseSerializerType.AssemblyQualifiedName, attribute.SerializerBaseTypeName);
        }
Exemple #4
0
        public void GenerateCodeForSmartPartInfoCompiles()
        {
            DesignerSerializerAttribute attr = (DesignerSerializerAttribute)Attribute.GetCustomAttribute(
                typeof(SmartPartInfo), typeof(DesignerSerializerAttribute));
            CodeDomSerializer serializer = (CodeDomSerializer)Activator.CreateInstance(Type.GetType(attr.SerializerTypeName));

            UserControl smartPart = new UserControl();

            Container     container = new Container();
            SmartPartInfo info1     = new SmartPartInfo("foo", "");

            container.Add(info1, "info1");
            MockSPI info2 = new MockSPI("bar", "");

            container.Add(info2, "info2");

            MockManager manager = new MockManager(container.Components);

            manager.Services.Add(typeof(IDesignerHost), new MockDesignerHost(smartPart, container));
            manager.Services.Add(typeof(IReferenceService), new MockReferenceService(container.Components));
            manager.Services.Add(typeof(IContainer), container);
            manager.Services.Add(typeof(IDesignerSerializationManager), manager);

            CodeTypeDeclaration declaration = new CodeTypeDeclaration("UserControl1");
            CodeMemberMethod    init        = new CodeMemberMethod();

            init.Name = "InitializeComponent";
            declaration.Members.Add(init);
            // Add empty fields for both SPIs.
            declaration.Members.Add(new CodeMemberField(typeof(ISmartPartInfo), "info1"));
            declaration.Members.Add(new CodeMemberField(typeof(ISmartPartInfo), "info2"));

            manager.Services.Add(typeof(CodeTypeDeclaration), declaration);

            serializer.Serialize(manager, info1);
            serializer.Serialize(manager, info2);

            StringWriter sw = new StringWriter();

            new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromType(
                declaration, sw, new CodeGeneratorOptions());

            sw.Flush();

            //Console.WriteLine(sw.ToString());

            CompilerResults results = new Microsoft.CSharp.CSharpCodeProvider().CompileAssemblyFromSource(
                new CompilerParameters(new string[] {
                "System.dll",
                new Uri(typeof(SmartPartInfo).Assembly.CodeBase).LocalPath
            }),
                sw.ToString());

            Assert.IsFalse(results.Errors.HasErrors, ErrorsToString(results.Errors));
        }
        protected CodeDomSerializer GetConfiguredSerializer(IDesignerSerializationManager manager, object value)
        {
            object[] attrs = value.GetType().GetCustomAttributes(typeof(DesignerSerializerAttribute), true);
            if (attrs.Length == 0)
            {
                return(null);
            }
            DesignerSerializerAttribute serializer = (DesignerSerializerAttribute)attrs[0];

            return((CodeDomSerializer)
                   Activator.CreateInstance(((ITypeResolutionService)manager.GetService(typeof(ITypeResolutionService))).GetType(serializer.SerializerTypeName)));
        }
Exemple #6
0
        public void TypeId_NullBaseSerializerTypeName_ThrowsNullReferenceException()
        {
            var attribute = new DesignerSerializerAttribute("SerializerType", (string)null);

            if (!PlatformDetection.IsFullFramework)
            {
                Assert.Equal("System.ComponentModel.Design.Serialization.DesignerSerializerAttribute", attribute.TypeId);
            }
            else
            {
                Assert.Throws <NullReferenceException>(() => attribute.TypeId);
            }
        }
        protected CodeDomSerializer GetSerializer(IDesignerSerializationManager manager, object instance)
        {
            DesignerSerializerAttribute attrInstance, attrType;

            attrType = attrInstance = null;

            CodeDomSerializer serializer = null;

            if (instance == null)
            {
                serializer = this.GetSerializer(manager, null);
            }
            else
            {
                AttributeCollection attributes = TypeDescriptor.GetAttributes(instance);
                foreach (Attribute a in attributes)
                {
                    DesignerSerializerAttribute designerAttr = a as DesignerSerializerAttribute;
                    if (designerAttr != null && manager.GetType(designerAttr.SerializerBaseTypeName) == typeof(CodeDomSerializer))
                    {
                        attrInstance = designerAttr;
                        break;
                    }
                }

                attributes = TypeDescriptor.GetAttributes(instance.GetType());
                foreach (Attribute a in attributes)
                {
                    DesignerSerializerAttribute designerAttr = a as DesignerSerializerAttribute;
                    if (designerAttr != null && manager.GetType(designerAttr.SerializerBaseTypeName) == typeof(CodeDomSerializer))
                    {
                        attrType = designerAttr;
                        break;
                    }
                }

                // if there is metadata modification in the instance then create the specified serializer instead of the one
                // in the Type.
                if (attrType != null && attrInstance != null && attrType.SerializerTypeName != attrInstance.SerializerTypeName)
                {
                    serializer = Activator.CreateInstance(manager.GetType(attrInstance.SerializerTypeName)) as CodeDomSerializer;
                }
                else
                {
                    serializer = this.GetSerializer(manager, instance.GetType());
                }
            }

            return(serializer);
        }
Exemple #8
0
            public object GetSerializer(Type objectType, Type serializerType)
            {
                DesignerSerializerAttribute attr = (DesignerSerializerAttribute)Attribute.GetCustomAttribute(
                    objectType, typeof(DesignerSerializerAttribute));

                if (attr == null)
                {
                    if (objectType == typeof(Component))
                    {
                        Type t = Type.GetType("System.ComponentModel.Design.Serialization.ComponentCodeDomSerializer, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
                        return(Activator.CreateInstance(t));
                    }
                    return(null);
                }
                else
                {
                    return((CodeDomSerializer)Activator.CreateInstance(Type.GetType(attr.SerializerTypeName)));
                }
            }
Exemple #9
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);
        }
Exemple #11
0
        public void DesignerAttributes_DesignerSerializerAttribute_TypeExists(Type annotatedType, DesignerSerializerAttribute attribute)
        {
            var type = Type.GetType(attribute.SerializerTypeName, false);

            _output.WriteLine($"{annotatedType.FullName}: {attribute.SerializerTypeName} --> {type?.FullName}");

            if (SkipList.Contains(attribute.SerializerTypeName))
            {
                Assert.Null(type);
                return;
            }

            Assert.NotNull(type);
        }
Exemple #12
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);
        }
        public void TypeId_NullBaseSerializerTypeName_ThrowsNullReferenceException()
        {
            var attribute = new DesignerSerializerAttribute("SerializerType", (string)null);

            Assert.Throws <NullReferenceException>(() => attribute.TypeId);
        }
Exemple #14
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);
        }