コード例 #1
0
ファイル: Config.cs プロジェクト: satish049/JsonConfig
        static Config()
        {
            // static C'tor, run once to check for compiled/embedded config

            // scan ALL linked assemblies and merge their default configs while
            // giving the entry assembly top priority in merge
            var executionPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            if (executionPath != null)
            {
                var exeFolder           = new DirectoryInfo(executionPath);
                var pluginAssemblyFiles = exeFolder.GetFiles("*.dll", SearchOption.AllDirectories);
                var currentAssemblyName = Assembly.GetEntryAssembly().ManifestModule.Name;
                var assemblies          = new List <Assembly>();
                foreach (var pluginAssemblyFile in pluginAssemblyFiles)
                {
                    if (currentAssemblyName.Equals(pluginAssemblyFile.Name))
                    {
                        continue;
                    }
                    var asm = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(pluginAssemblyFile.FullName);
                    assemblies.Add(asm);
                }

                var entryAssembly = Assembly.GetEntryAssembly();

                foreach (var assembly in assemblies.Where(assembly => !assembly.Equals(entryAssembly)))
                {
                    Default = Merger.Merge(GetDefaultConfig(assembly), Default);
                }
                if (entryAssembly != null)
                {
                    Default = Merger.Merge(GetDefaultConfig(entryAssembly), Default);
                }
            }

            // User config (provided through a settings.conf file)
            var userConfigFilename = "settings";

            // TODO this is ugly but makes life easier
            // we are run from the IDE, so the settings.conf needs
            // to be searched two levels up
            var settingsPath   = GetUserSettingsFolder(executionPath);
            var settingsFolder = new DirectoryInfo(settingsPath);
            var userConfig     = (from FileInfo fi in settingsFolder.GetFiles()
                                  where (
                                      fi.FullName.EndsWith(userConfigFilename + ".conf") ||
                                      fi.FullName.EndsWith(userConfigFilename + ".json") ||
                                      fi.FullName.EndsWith(userConfigFilename + ".conf.json") ||
                                      fi.FullName.EndsWith(userConfigFilename + ".json.conf")
                                      )
                                  select fi).FirstOrDefault();

            if (userConfig != null)
            {
                User = ParseJson(File.ReadAllText(userConfig.FullName));
                WatchUserConfig(userConfig);
            }
            else
            {
                User = new NullExceptionPreventer();
            }
        }
コード例 #2
0
 object Clone()
 {
     return(Merger.Merge(new ConfigObject(), this));
 }