2015-05-13 27 views
6

Linqpad是唯一可以使用我的大型代碼庫的腳本環境。它是唯一的環境適用於我的原因是,其他解決方案(蟒蛇等)總是無法正確加載配置從app.config,但linqpad.config工程我可以編程分配要使用的linqpad配置文件嗎?

問題是,我有多個配置文件,每我希望將其中的一個用於腳本,我必須轉到linqpad安裝目錄,將正確的配置文件重命名爲linqpad.config並重新啓動linq。生產力很差。

我試圖通過設置文件:

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\Shared\app.config"); 

,但沒有任何效果。有沒有什麼辦法可以改變將在Linqpad的C#程序的Main()方法中使用的配置文件?如果我可以調用一個函數來設置要使用的配置文件,然後運行我的代碼的其餘部分,那很好。

+0

而不是重命名的配置文件並重新啓動LINQPad,你有沒有考慮創建單獨的文件夾,每個都有所需的配置文件和LINQPad.exe的副本? –

+0

這並不能真正提高我的工作效率。當我想要創建一個應該使用特定配置文件的新查詢時,包括調用頂部的調用會比使用exe + config創建新文件夾快得多。這就是爲什麼我特意詢問是否程序設置是可能的。 – mahonya

+2

我明白了。現在不可能,但在TODO清單上。 LINQPad將來會允許通過查詢屬性選項。儘管如此,它永遠不會以編程方式實現。腳本運行時,已經太晚了。 –

回答

9

例LINQ查詢:

void Main() 
{ 
    var configPath = Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), "app.config"); 

    // Load custom config 
    var configProxy = new ConfigurationProxy(configPath); 
    if (!configProxy.InjectToConfigurationManager()) 
    { 
     Trace.TraceError("Cannot load config from " + configPath); 
     throw new InvalidOperationException("Cannot load config " + configPath); 
    } 

    ConfigurationManager.AppSettings["LogPath"].Dump(); 
} 

添加這個類到你的Linq查詢:

/// <summary> 
/// A custom config injector. 
/// </summary> 
public sealed class ConfigurationProxy : IInternalConfigSystem 
{ 
    /// <summary> 
    /// The custom sections 
    /// </summary> 
    private readonly Dictionary<string, IConfigurationSectionHandler> customSections; 

    /// <summary> 
    /// The configuration 
    /// </summary> 
    private Configuration config; 

    /// <summary> 
    /// Initializes a new instance of the <see cref="ConfigurationProxy"/> class. 
    /// </summary> 
    /// <param name="fileName"> 
    /// Name of the file. 
    /// </param> 
    /// <remarks> 
    /// this is called filename but really it's the path as needed 
    /// it defaults to checking the directory you're running in. 
    /// </remarks> 
    public ConfigurationProxy(string fileName) 
    { 
     this.customSections = new Dictionary<string, IConfigurationSectionHandler>(); 

     if (!this.Load(fileName)) 
     { 
      throw new ConfigurationErrorsException(string.Format(CultureInfo.CurrentCulture, "File: {0} could not be found or was not a valid configuration file.", this.config.FilePath)); 
     } 
    } 

    /// <summary> 
    /// Gets the configuration. 
    /// </summary> 
    public Configuration Configuration 
    { 
     get 
     { 
      return this.config; 
     } 
    } 

    /// <summary> 
    /// Returns the configuration object based on the specified key. 
    /// </summary> 
    /// <param name="configKey">The configuration key value.</param> 
    /// <returns> 
    /// A configuration object. 
    /// </returns> 
    public object GetSection(string configKey) 
    { 
     if (configKey == "appSettings") 
     { 
      return this.BuildAppSettings(); 
     } 

     object sect = this.config.GetSection(configKey); 

     if (this.customSections.ContainsKey(configKey) && sect != null) 
     { 
      var xml = new XmlDocument(); 

      xml.LoadXml(((ConfigurationSection)sect).SectionInformation.GetRawXml()); 

      // I have no idea what I should normally be passing through in the first 
      // two params, but I never use them in my config handlers so I opted not to 
      // worry about it and just pass through something... 
      sect = this.customSections[configKey].Create(this.config, this.config.EvaluationContext, xml.FirstChild); 
     } 

     return sect; 
    } 

    /// <summary> 
    /// The refresh config. 
    /// </summary> 
    /// <param name="sectionName"> 
    /// The section name. 
    /// </param> 
    public void RefreshConfig(string sectionName) 
    { 
     // I suppose this will work. Reload the whole file? 
     this.Load(this.config.FilePath); 
    } 

    /// <summary> 
    /// Gets a value indicating whether supports user config. 
    /// </summary> 
    public bool SupportsUserConfig 
    { 
     get 
     { 
      return false; 
     } 
    } 

    /// <summary> 
    /// Injects to configuration manager. 
    /// </summary> 
    /// <returns>Whether the configuration was injected</returns> 
    public bool InjectToConfigurationManager() 
    { 
     // inject self into ConfigurationManager 
     var configSystem = typeof(ConfigurationManager).GetField("s_configSystem", BindingFlags.Static | BindingFlags.NonPublic); 
     if (configSystem != null) 
     { 
      configSystem.SetValue(null, this); 
     } 

     // lame check, but it's something 
     if (ConfigurationManager.AppSettings.Count == this.config.AppSettings.Settings.Count) 
     { 
      return true; 
     } 

     return false; 
    } 

    /// <summary> 
    /// Loads the specified file. 
    /// </summary> 
    /// <param name="file"> 
    /// The file. 
    /// </param> 
    /// <returns> 
    /// Is file loaded 
    /// </returns> 
    private bool Load(string file) 
    { 
     var map = new ExeConfigurationFileMap { ExeConfigFilename = file }; 
     this.config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 

     var xml = new XmlDocument(); 
     using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read)) 
     { 
      xml.Load(stream); 
     } 

     // var cfgSections = xml.GetElementsByTagName("configSections"); 

     // if (cfgSections.Count > 0) 
     // { 
     // foreach (XmlNode node in cfgSections[0].ChildNodes) 
     // { 
     // var type = System.Activator.CreateInstance(
     // Type.GetType(node.Attributes["type"].Value)) 
     // as IConfigurationSectionHandler; 

     // if (type == null) continue; 

     // customSections.Add(node.Attributes["name"].Value, type); 
     // } 
     // } 
     return this.config.HasFile; 
    } 

    /// <summary> 
    /// The build app settings. 
    /// </summary> 
    /// <returns> 
    /// The <see cref="NameValueCollection"/>. 
    /// </returns> 
    private NameValueCollection BuildAppSettings() 
    { 
     var coll = new NameValueCollection(); 

     foreach (var key in this.config.AppSettings.Settings.AllKeys) 
     { 
      coll.Add(key, this.config.AppSettings.Settings[key].Value); 
     } 

     return coll; 
    } 
} 
+1

效果很好。您必須添加命名空間,System.Collections.Specialized System.Configuration System.Configuration.Internal。您還必須添加對System.Configuration.dll的引用。 – Jim

相關問題