第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在運行時更改默認(rèn)app.config

在運行時更改默認(rèn)app.config

SMILET 2019-06-28 16:18:11
在運行時更改默認(rèn)app.config我有以下問題:我們有一個加載模塊的應(yīng)用程序(添加ON)。這些模塊可能需要app.config中的條目(例如,WCF配置)。由于模塊是動態(tài)加載的,所以我不希望應(yīng)用程序的app.config文件中有這些條目。我想做的是:在內(nèi)存中創(chuàng)建一個新的app.config,它包含模塊中的配置部分告訴我的應(yīng)用程序使用那個新app.config注意:我不想覆蓋默認(rèn)app.config!它應(yīng)該透明地工作,例如ConfigurationManager.AppSettings使用那個新文件。在我評估這個問題時,我想出了與這里提供的相同的解決方案:用nunit重新加載app.config.不幸的是,它似乎什么也不做,因為我仍然從普通app.config獲取數(shù)據(jù)。我使用這個代碼來測試它:Console.WriteLine(ConfigurationManager.AppSettings["SettingA"]);Console.WriteLine(Settings.Default.Setting);var combinedConfig = string.Format(CONFIG2, CONFIG);var tempFileName = Path.GetTempFileName();using (var writer = new StreamWriter(tempFileName)){     writer.Write(combinedConfig);}using(AppConfig.Change(tempFileName)){     Console.WriteLine(ConfigurationManager.AppSettings["SettingA"]);     Console.WriteLine(Settings.Default.Setting);}它打印相同的值,盡管combinedConfig包含普通app.config以外的其他值。
查看完整描述

3 回答

?
有只小跳蛙

TA貢獻(xiàn)1824條經(jīng)驗 獲得超8個贊

如果在第一次使用配置系統(tǒng)之前就使用了鏈接問題,那么鏈接問題中的黑客就能工作。在那之后,它就不再起作用了。
原因:
存在一個類ClientConfigPaths隱藏路徑。所以,即使在更改路徑之后SetData,因為已經(jīng)存在緩存的值,所以不會重讀。解決辦法也是刪除這些內(nèi)容:

using System;using System.Configuration;using System.Linq;using System.Reflection;public abstract class AppConfig : IDisposable{
    public static AppConfig Change(string path)
    {
        return new ChangeAppConfig(path);
    }

    public abstract void Dispose();

    private class ChangeAppConfig : AppConfig
    {
        private readonly string oldConfig =
            AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString();

        private bool disposedValue;

        public ChangeAppConfig(string path)
        {
            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
            ResetConfigMechanism();
        }

        public override void Dispose()
        {
            if (!disposedValue)
            {
                AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", oldConfig);
                ResetConfigMechanism();


                disposedValue = true;
            }
            GC.SuppressFinalize(this);
        }

        private static void ResetConfigMechanism()
        {
            typeof(ConfigurationManager)
                .GetField("s_initState", BindingFlags.NonPublic | 
                                         BindingFlags.Static)
                .SetValue(null, 0);

            typeof(ConfigurationManager)
                .GetField("s_configSystem", BindingFlags.NonPublic | 
                                            BindingFlags.Static)
                .SetValue(null, null);

            typeof(ConfigurationManager)
                .Assembly.GetTypes()
                .Where(x => x.FullName == 
                            "System.Configuration.ClientConfigPaths")
                .First()
                .GetField("s_current", BindingFlags.NonPublic | 
                                       BindingFlags.Static)
                .SetValue(null, null);
        }
    }}

用法如下:

// the default app.config is used.using(AppConfig.Change(tempFileName)){
    // the app.config in tempFileName is used}// the default app.config is used.

如果要更改應(yīng)用程序整個運行時使用的app.config,只需AppConfig.Change(tempFileName)在您的應(yīng)用程序開始時不需要在某個地方使用。


查看完整回答
反對 回復(fù) 2019-06-28
  • 3 回答
  • 0 關(guān)注
  • 841 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號