public void RegisterAssembly(Assembly assembly, IAttributeRegistry attributeRegistry)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }
            if (attributeRegistry == null)
            {
                throw new ArgumentNullException(nameof(attributeRegistry));
            }

            // Add automatically the assembly for lookup
            if (!lookupAssemblies.Contains(assembly))
            {
                lookupAssemblies.Add(assembly);

                // Register all tags automatically.
                var assemblySerializers = DataSerializerFactory.GetAssemblySerializers(assembly);
                if (assemblySerializers != null)
                {
                    foreach (var dataContractAlias in assemblySerializers.DataContractAliases)
                    {
                        RegisterTagMapping(dataContractAlias.Name, dataContractAlias.Type, dataContractAlias.IsAlias);
                    }
                }
                else
                {
                    Log.Warning($"Assembly [{assembly}] has not been processed by assembly processor with --serialization flags. [DataContract] aliases won't be available.");
                }
                // Automatically register YamlSerializableFactory
                var assemblyScanTypes = AssemblyRegistry.GetScanTypes(assembly);
                if (assemblyScanTypes != null)
                {
                    List <Type> types;

                    // Register serializer factories
                    if (assemblyScanTypes.Types.TryGetValue(typeof(IYamlSerializableFactory), out types))
                    {
                        foreach (var type in types)
                        {
                            if (typeof(IYamlSerializableFactory).IsAssignableFrom(type) && type.GetConstructor(Type.EmptyTypes) != null)
                            {
                                try
                                {
                                    SerializableFactories.Add((IYamlSerializableFactory)Activator.CreateInstance(type));
                                }
                                catch
                                {
                                    // Registrying an assembly should not fail, so we are silently discarding a factory if
                                    // we are not able to load it.
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        public void RegisterAssembly(Assembly assembly, IAttributeRegistry attributeRegistry)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }
            if (attributeRegistry == null)
            {
                throw new ArgumentNullException("attributeRegistry");
            }

            // Add automatically the assembly for lookup
            if (!DefaultLookupAssemblies.Contains(assembly) && !lookupAssemblies.Contains(assembly))
            {
                lookupAssemblies.Add(assembly);

                var types = new Type[0];

                // Register all tags automatically.
                foreach (var type in assembly.GetTypes())
                {
                    var tagAttribute = attributeRegistry.GetAttribute <YamlTagAttribute>(type);
                    if (tagAttribute != null && !string.IsNullOrEmpty(tagAttribute.Tag))
                    {
                        RegisterTagMapping(tagAttribute.Tag, type);
                    }

                    // Automatically register YamlSerializableFactory
                    if (typeof(IYamlSerializableFactory).IsAssignableFrom(type) && type.GetConstructor(types) != null)
                    {
                        try
                        {
                            SerializableFactories.Add((IYamlSerializableFactory)Activator.CreateInstance(type));
                        }
                        catch
                        {
                            // Registrying an assembly should not fail, so we are silently discarding a factory if
                            // we are not able to load it.
                        }
                    }
                }
            }
        }
Пример #3
0
        public void RegisterAssembly(Assembly assembly, IAttributeRegistry attributeRegistry)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }
            if (attributeRegistry == null)
            {
                throw new ArgumentNullException("attributeRegistry");
            }

            // Add automatically the assembly for lookup
            if (!DefaultLookupAssemblies.Contains(assembly) && !lookupAssemblies.Contains(assembly))
            {
                lookupAssemblies.Add(assembly);

                var types = new Type[0];

                // Register all tags automatically.
                foreach (var type in assembly.GetTypes())
                {
                    var attributes = attributeRegistry.GetAttributes(type.GetTypeInfo());
                    foreach (var attribute in attributes)
                    {
                        string name         = null;
                        bool   isAlias      = false;
                        var    tagAttribute = attribute as YamlTagAttribute;
                        if (tagAttribute != null)
                        {
                            name = tagAttribute.Tag;
                        }
                        else
                        {
                            var yamlRemap = attribute as YamlRemapAttribute;
                            if (yamlRemap != null)
                            {
                                name    = yamlRemap.Name;
                                isAlias = true;
                            }
                        }
                        if (!string.IsNullOrEmpty(name))
                        {
                            RegisterTagMapping(name, type, isAlias);
                        }
                    }

                    // Automatically register YamlSerializableFactory
                    if (typeof(IYamlSerializableFactory).IsAssignableFrom(type) && type.GetConstructor(types) != null)
                    {
                        try
                        {
                            SerializableFactories.Add((IYamlSerializableFactory)Activator.CreateInstance(type));
                        }
                        catch
                        {
                            // Registrying an assembly should not fail, so we are silently discarding a factory if
                            // we are not able to load it.
                        }
                    }
                }
            }
        }