C# 交互式窗口可以与我的代码交互吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11135571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:39:01  来源:igfitidea点击:

Can the C# interactive window interact with my code?

c#.netvisual-studioroslyn

提问by Colonel Panic

In Visual Studio 2015 or later, I can open the 'C# interactive window', and run code:

在 Visual Studio 2015 或更高版本中,我可以打开“C# 交互窗口”并运行代码:

> 5 + 3
8

That's cute. Now how can I interact my code—my classes? Assume I have a project open.

那很可爱。现在我该如何交互我的代码——我的类?假设我有一个项目打开。

> new Cog()
(1,5): error CS0246: The type or namespace name 'Cog' could not be found (are you missing a using directive or an assembly reference?)

采纳答案by sloth

When using Visual Studio 2015:

使用 Visual Studio 2015 时:

You can open the Interactive window by navigating to Views > Other Windows > C# Interactive,

您可以通过导航到Views > Other Windows > C# Interactive来打开 Interactive 窗口,

Then just right click your project and run Initialize Interactive with Projectfrom the context menu.

然后只需右键单击您的项目并从上下文菜单中运行Initialize Interactive with Project



For older versions:

对于旧版本:

To use the C# Interactive Window with your code, ensure you first compile your code, then add a reference to the resulting assembly with the #rcommand like so:

要在代码中使用 C# 交互式窗口,请确保首先编译代码,然后使用如下#r命令添加对生成的程序集的引用:

C# command prompt

C# 命令提示符



You can also use the Immediate Windowto interact with your code as well, like so:

您还可以使用即时窗口与您的代码进行交互,如下所示:

Immediate Window

即时窗口

回答by Botz3000

You can use classes from your own project.
Just right click on your solution and select "Reset Interactive from Project".

您可以使用自己项目中的类。
只需右键单击您的解决方案并选择“从项目重置交互”。

If you need more information, here is the source:
Using the C# Interactive Window that comes with Roslyn – Part 2

如果您需要更多信息,这里是来源:
使用 Roslyn 附带的 C# 交互窗口 – 第 2 部分

回答by Luke Hammer

Just an update from the @Botz3000 answer.

只是来自@Botz3000 答案的更新。

The command you want to find is now called "Initialize Interactive with Project"

您要查找的命令现在称为“Initialize Interactive with Project”

enter image description here

在此处输入图片说明

Also it is worth noting i could not find this command if my C# interactive window was not viewable.

另外值得注意的是,如果我的 C# 交互式窗口不可见,我将找不到此命令。

回答by beloblotskiy

Totally agree "Initialize Interactive with Project" is cool.

完全同意“用项目初始化交互”很酷。

My approach is to push classes into a library and use //css_referencein C# script or #rin C# Interactive window

我的做法是推类到库和使用 // css_reference在C#脚本或r组成的C#互动窗口

For example:

例如:

#r "D:\dev\DbHMonData\LoadH2Stats\bin\Debug\DbHMonStats.dll"
using DbHMonStats;

回答by Mahmoud Hanafy

It's worth noting that the feature isn't yet supported in VS 2019 for .Net Core project.

值得注意的是,VS 2019 for .Net Core 项目尚不支持该功能。

You won't find the option, and it's a known issue as highlighted in this answer '"Initialize interactive with Project" is missing for .Net Core Projects in Visual Studio 2019'

您将找不到该选项,这是此答案中突出显示的已知问题“Visual Studio 2019 中.Net Core 项目缺少“与项目的交互初始化”

The workaround is to use #r command(#r "Path/MyDll.dll") to load the assembly manually as seen in the answer above.

解决方法是使用 #r command(#r "Path/MyDll.dll") 手动加载程序集,如上面的答案所示。