Esempio n. 1
0
        public static Settings Load(TypeResolver typeResolver, XElement config)
        {
            var settings = new Settings();

            foreach (CustomAttribute attribute in typeResolver.GetAllModules().SelectMany(m => m.Assembly.CustomAttributes))
            {
                if (attribute.AttributeType.IsType <SettingsAttribute>())
                {
                    foreach (CustomAttributeNamedArgument property in attribute.Properties)
                    {
                        if (property.Argument.Value != null)
                        {
                            switch (property.Name)
                            {
                            case nameof(SettingsAttribute.AutoInit):
                                settings.AutoInit = (bool)property.Argument.Value;
                                break;

                            case nameof(SettingsAttribute.Behavior):
                                settings.Behavior = (Behaviors)property.Argument.Value;
                                break;

                            case nameof(SettingsAttribute.DebugLogLevel):
                                settings.DebugLogLevel = (DebugLogLevel)property.Argument.Value;
                                break;

                            case nameof(SettingsAttribute.GenerateRegistrations):
                                settings.GenerateRegistrations = (bool)property.Argument.Value;
                                break;

                            case nameof(SettingsAttribute.DebugCodeGeneration):
                                settings.DebugCodeGeneration = (CodeLanguage)property.Argument.Value;
                                break;
                            }
                        }
                    }
                }
                else if (attribute.AttributeType.IsType <MapAttribute>())
                {
                    Lifetime?lifetime = null;
                    string   pattern  = null;
                    foreach (CustomAttributeArgument ctorArgument in attribute.ConstructorArguments)
                    {
                        if (ctorArgument.Type.IsType <Lifetime>())
                        {
                            lifetime = (Lifetime)ctorArgument.Value;
                        }
                        else if (ctorArgument.Type.IsType <string>())
                        {
                            pattern = (string)ctorArgument.Value;
                        }
                        else if (ctorArgument.Type.IsType <Type>())
                        {
                            pattern = ((TypeDefinition)ctorArgument.Value).FullName;
                        }
                    }

                    if (lifetime != null && pattern != null)
                    {
                        settings.Types.Add(new MatchType(pattern, lifetime.Value));
                    }
                    else
                    {
                        throw new SettingsParseException($"Assembly level {nameof(MapAttribute)} must contain both a {nameof(MapAttribute.Lifetime)} and a {nameof(MapAttribute.TargetTypePattern)}");
                    }
                }
            }

            return(Parse(settings, config));
        }