gpt4 book ai didi

linqpad - 我可以以编程方式分配要使用的 linqpad 配置文件吗?

转载 作者:行者123 更新时间:2023-12-04 11:23:11 29 4
gpt4 key购买 nike

Linqpad 是唯一一个类似脚本的环境,我可以在其中使用我的大型代码库。它是唯一适合我的环境的原因是其他解决方案(ironpython 等)总是无法从 app.config 正确加载配置,但 linqpad.config 有效

问题是,我有多个配置文件,每次我想将其中一个用于脚本时,我必须转到 linqpad 安装目录,将正确的配置文件重命名为 linqpad.config 并重新启动 linq。几乎没有生产力。

我试图通过以下方式设置文件:

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

但它没有任何效果。有什么方法可以更改将在 Linqpad 中 C# 程序的 Main() 方法中使用的配置文件?如果我可以调用一个函数来设置要使用的配置文件,然后运行我的其余代码,那就太好了。

最佳答案

示例 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;
}
}

关于linqpad - 我可以以编程方式分配要使用的 linqpad 配置文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30208625/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com