简体   繁体   English

我可以在不编译整个项目的情况下从即时 Window 调用 C# function 吗?

[英]Can I call a C# function from the Immediate Window without compiling my whole project?

Back in my VB6 days, I could use the Immediate Window to run a quick sanity test on a function, even if the rest of my code was in a broken state: Back in my VB6 days, I could use the Immediate Window to run a quick sanity test on a function, even if the rest of my code was in a broken state:

在此处输入图像描述

It was handy for quick and dirty REPL debugging.它对于快速和肮脏的 REPL 调试非常方便。

When writing C# code in Visual Studio, I can't call any of my code from the Immediate Window unless all of it is valid and compiles.在 Visual Studio 中编写 C# 代码时,我无法从即时 Window 调用我的任何代码,除非所有代码都有效且可以编译。

That works fine when my project is in a ready to run state, but not when I'm in the midst of tearing apart a bunch of code.当我的项目准备好运行 state 时,这很好用,但当我正在拆解一堆代码时就不行了。

Is there any way I can configure the Immediate Window to only compile what's needed for the statement I'm trying to execute?有什么方法可以配置即时 Window 以仅编译我要执行的语句所需的内容? Or other workarounds?还是其他解决方法?

I've heard LINQPad is awesome, but I assume it would require me to copy-paste all the code my test statement depends on before I can run it.我听说 LINQPad 很棒,但我认为它需要我复制粘贴我的测试语句所依赖的所有代码,然后才能运行它。 I've also played with the newer Interactive Window but don't think it does what I'm hunting for.我也玩过更新的交互式 Window,但不认为它符合我的要求。

From your comment above, you already stumbled on the answer: The C# Interactive window ( View | Other Windows | C# Interactive ) is the feature you want. From your comment above, you already stumbled on the answer: The C# Interactive window ( View | Other Windows | C# Interactive ) is the feature you want. If the Immediate Window isn't able to run the code you want to run due to existing project errors, then C# Interactive will.如果即时 Window 由于现有项目错误而无法运行您要运行的代码,则 C# Interactive 将。 Visual Studio's C# Interactive window actually works the same way VB6's Immediate Window works. Visual Studio 的 C# Interactive window 实际上与 VB6 的即时 Window 的工作方式相同。 The C# Interactive window will successfully compile and run code that does not depend on your error-state project code: C# Interactive window 将成功编译和运行不依赖于错误状态项目代码的代码:

在此处输入图像描述

And just like in VB6, you wouldn't be able to access a function or variable from project code that is not compiling--as others have pointed out, how would the compiler possibly work in any "immediate-window" mode if it had to depend on code that cannot compile?就像在 VB6 中一样,您将无法从未编译的项目代码中访问 function 或变量——正如其他人指出的那样,如果编译器有依赖无法编译的代码? By as my image above proves, as long as the code you present to the C# Interactive window is compilable, it will work even when your project code will not.正如我上面的图片所证明的那样,只要您提供给 C# 交互式 window 的代码是可编译的,即使您的项目代码不能编译,它也可以工作。

在此处输入图像描述

As long as you steer clear of any code that might depend on the project code, you even can compile more complex examples, like this:只要您避开任何可能依赖于项目代码的代码,您甚至可以编译更复杂的示例,如下所示:

However, unlike VB6's Immediate window (which is a sort of VB-only hybrid of Visual Studio's Immediate along with Visual Studio's C#/F# Interactive window), you cannot reference values that exist in project code because the C#/F# Interactive window is sandboxed.但是,与 VB6 的即时 window(Visual Studio 的即时与 Visual Studio 的 C#/F# 交互窗口的一种混合)不同,您不能引用项目代码中存在的值,因为 C#/F# 交互 window 是沙盒。 Thus, in the examples shown, I cannot access ggc.Name in the C# Interactive window--that window doesn't have access to any project variables or state--whether or not the project has compilation errors.因此,在所示示例中,我无法访问ggc.Name交互式窗口中的 ggc.Name —— window 无权访问任何项目变量或状态——无论项目是否存在编译错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在自己的C#项目中调用FontForge的某些功能吗? - Can I call some function of FontForge in my own C# project? 我可以使用C#Interactive窗口或立即窗口与当前调试会话变量进行交互吗? - Can I interact with the current debugging session variables using C# Interactive window or Immediate window? 如何编写我的 C++ 函数以便我可以从 C# 调用它? - How to write my C++ function so I can call it from C#? 如何在C#的立即窗口的Visual Studio 2017中加载SOS? - How can I load SOS in Visual Studio 2017 in Immediate Window in C#? 如何从C#调用C ++函数? - How can I call a C++ function from C#? 我怎样才能从datagridview中的选定行获得整个对象而不显示对象ID -c# - how i can get the whole objected from the selected row in datagridview without displaying the object ID -c# 将整个程序与我的C#项目合并 - Merge whole program with my C# project 如何判断我的代码是否已从立即窗口调用? - How can I tell if my code has been called from the Immediate Window? 我如何从 C# 中的另一个方法调用这个 function? - How can I call this function from another method in C#? 整个项目C#中可用的功能 - Function available in whole project C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM