LINQPad中如何模拟ConfigurationManager
How to simulate ConfigurationManager in LINQPad
我正在尝试在 LINQPad 中测试一些代码。但是,base
class 调用配置管理器。在 LINQPad 中测试时如何模拟它。
void Main()
{
var tRepo = new TestRepository();
var result = tRepo.GetAsync(1);
result.Dump();
}
public partial class TestRepository : BaseRepository<Customer>, ICustomerRepository
{
// Here base throws the errror
public TestRepository() : base("DbConnString")
{
}
}
这是 BaseRepository 的构造函数(来自已编译的 DLL,因此在 LINQPad 中不可编辑):
protected BaseRepository(string connectionStringName)
{
var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName];
Connection = new SqlConnection(connectionString.ConnectionString);
Connection.Open();
}
可以在 LinqPad 网站常见问题解答中找到答案
http://www.linqpad.net/faq.aspx
I'm referencing a custom assembly that reads settings from an application configuration file (app.config). Where should I put my application config file so that LINQPad queries will pick it up?
Create a file called linqpad.config in the same folder as LINQPad.exe and put your configuration data there. Don't confuse this with linqpad.exe.config:
•linqpad.exe.config is for the LINQPad GUI
•linqpad.config is for your queries.
可能对你有用的东西,我前段时间创建的。
这是一种扩展方法,您可以使用它来强制从特定文件重新加载配置。它使用反射来更改管理器中的私有字段,清除配置,然后有条件地重新加载它。比手动编辑 LINQPad 的配置文件要容易得多。
public static void ForceNewConfigFile(this Type type, bool initialize = true)
{
var path = type.Assembly.Location + ".config";
if (!File.Exists(path))
throw new Exception("Cannot find file " + path + ".");
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
var typeOfConfigManager = typeof(ConfigurationManager);
typeOfConfigManager.GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, 0);
typeOfConfigManager.GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, null);
var typeOfClientConfigPaths = typeOfConfigManager.Assembly.GetTypes().Where(x => x.FullName == "System.Configuration.ClientConfigPaths").Single();
typeOfClientConfigPaths.GetField("s_current", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, null);
if (initialize)
{
var dummy = ConfigurationManager.AppSettings;
}
}
用法示例:
typeof(SomeType).ForceNewConfigFile();
System.Configuration.ConfigurationManager.AppSettings.Dump();
SomeType
只是程序集中包含的一种类型,它将用作配置文件位置的来源。假设是:配置文件存在于 DLL 文件旁边并命名为 {Assembly.Location}.config
.
我正在尝试在 LINQPad 中测试一些代码。但是,base
class 调用配置管理器。在 LINQPad 中测试时如何模拟它。
void Main()
{
var tRepo = new TestRepository();
var result = tRepo.GetAsync(1);
result.Dump();
}
public partial class TestRepository : BaseRepository<Customer>, ICustomerRepository
{
// Here base throws the errror
public TestRepository() : base("DbConnString")
{
}
}
这是 BaseRepository 的构造函数(来自已编译的 DLL,因此在 LINQPad 中不可编辑):
protected BaseRepository(string connectionStringName)
{
var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName];
Connection = new SqlConnection(connectionString.ConnectionString);
Connection.Open();
}
可以在 LinqPad 网站常见问题解答中找到答案
http://www.linqpad.net/faq.aspx
I'm referencing a custom assembly that reads settings from an application configuration file (app.config). Where should I put my application config file so that LINQPad queries will pick it up?
Create a file called linqpad.config in the same folder as LINQPad.exe and put your configuration data there. Don't confuse this with linqpad.exe.config:
•linqpad.exe.config is for the LINQPad GUI
•linqpad.config is for your queries.
可能对你有用的东西,我前段时间创建的。
这是一种扩展方法,您可以使用它来强制从特定文件重新加载配置。它使用反射来更改管理器中的私有字段,清除配置,然后有条件地重新加载它。比手动编辑 LINQPad 的配置文件要容易得多。
public static void ForceNewConfigFile(this Type type, bool initialize = true)
{
var path = type.Assembly.Location + ".config";
if (!File.Exists(path))
throw new Exception("Cannot find file " + path + ".");
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
var typeOfConfigManager = typeof(ConfigurationManager);
typeOfConfigManager.GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, 0);
typeOfConfigManager.GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, null);
var typeOfClientConfigPaths = typeOfConfigManager.Assembly.GetTypes().Where(x => x.FullName == "System.Configuration.ClientConfigPaths").Single();
typeOfClientConfigPaths.GetField("s_current", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, null);
if (initialize)
{
var dummy = ConfigurationManager.AppSettings;
}
}
用法示例:
typeof(SomeType).ForceNewConfigFile();
System.Configuration.ConfigurationManager.AppSettings.Dump();
SomeType
只是程序集中包含的一种类型,它将用作配置文件位置的来源。假设是:配置文件存在于 DLL 文件旁边并命名为 {Assembly.Location}.config
.