访问Roslyn REPL中的.config文件 [英] Access to .config files in Roslyn REPL

查看:44
本文介绍了访问Roslyn REPL中的.config文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用罗斯林2012年6月CTP :

有没有一种方法可以为Roslyn C#Interactive/REPL提供一个.config文件,供正在探索的代码使用?一个简单的示例场景是代码,该代码取决于通常从app.config/web.config获取的连接字符串.

Is there a way to provide the Roslyn C# Interactive/REPL with a .config file for the code being explored? A simple example scenario is code which depends on a connection string which it would usually obtain from the app.config/web.config.

推荐答案

现在实际上是可能的(可能在提出此问题时就已经存在).只需使用以下命令创建一个LoadConfig.csx文件:

This is actually possible now (and may have been at the time this question was asked). Simply create a LoadConfig.csx file with the following:

#r "System.Configuration"

using System;
using System.IO;
using System.Linq;


var paths = new[] { Path.Combine(Environment.CurrentDirectory, "Web.config"), Path.Combine(Environment.CurrentDirectory, "App.config") };
var configPath = paths.FirstOrDefault(p => File.Exists(p));

if (configPath != null)
{
    // Set new configuration path
    AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configPath);  

    // Reset current configuration load state
    var t = typeof(System.Configuration.ConfigurationManager);
    var f = t.GetField("s_initState", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
    f.SetValue(null, 0);
}

将文件保存在您会记得的地方,然后返回要在Visual Studio中加载的项目.右键单击该项目,然后选择从项目中重置C#Interactive".C#交互式窗口加载完毕后,请执行以下操作:

Save the file somewhere you'll remember and return to the project you want to load in visual studio. Right click the project and select "Reset C# Interactive from Project". Once the C# interactive window is finished loading call:

#load "C:\path\to\LoadConfig.csx"

注意:您必须在加载项目后立即调用此命令.如果在加载此脚本之前进行了任何配置查找,则该脚本将无法工作.

NOTE: You must call this immediately after loading the project. If any configuration lookups occur before you load this script then this script will fail to work.

现在,"C#交互式"窗口应该可以访问您的项目设置.

Now the C# Interactive window should have access to your project settings.

这篇关于访问Roslyn REPL中的.config文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆