简体   繁体   English

是否可以使用Main方法在C#中执行类似于Java中的任意类?

[英]Is it possible to execute an arbitrary class in C# with a Main method, similar to that in Java?

I've recently had to migrate to the C# world. 我最近不得不迁移到C#世界。 Coming from the Java land, I could add a public static void main(String[] args) method to any class and select to run that class from Eclipse/Netbeans for any code/logic that I wanted to quickly test. 来自Java领域,我可以向任何类添加一个public static void main(String[] args)方法,然后选择要从Eclipse / Netbeans中运行该类以获取想要快速测试的任何代码/逻辑。

Is there an equivalent of the same capability in C#.Net/Visual Studio? C#.Net / Visual Studio中是否有相同的功能? I've tried doing that and the best I can do is to execute it from the command prompt via csc.exe . 我尝试过这样做,最好的办法是通过csc.exe在命令提示符下执行它。 However, for some reason, it complains about not finding the relevant DLLs - it seems to expect to run that class in complete isolation without any dependency on "external code" (ie, code residing in that VS project/solution where the class resides). 但是,由于某种原因,它抱怨找不到相关的DLL-似乎期望完全隔离地运行该类,而无需依赖“外部代码”(即,该类所在的VS项目/解决方案中的代码) 。

Reason for this capability : All project files are marked as class libraries and sometimes I just wish to check if a particular set of methods/data/logic will work as expected with the current code base. 使用此功能的原因 :所有项目文件都标记为类库,有时我只想检查一组特定的方法/数据/逻辑是否可以在当前代码库中正常工作。 In Java, I'd quickly write it in the main method and execute that class to see how it goes prior to committing it to version control. 在Java中,我会在main方法中快速编写它,并执行该类以查看其运行方式,然后再将其提交给版本控制。 However, there seems to be no easy way to trigger the execution of "my class" with all dependencies correctly handled by csc.exe 但是,似乎没有简单的方法可以通过csc.exe正确处理所有依赖项来触发“ my class”的执行

Current Solution: Add this testing code to the unit test project and select to execute that particular "test" so as to check if the idea seems to work fine (it may fire DB calls or webservice class etc., and not be purely a logical flow of computation). 当前解决方案:将此测试代码添加到单元测试项目中,然后选择执行该特定的“测试”,以检查该想法是否运行良好(它可能会触发数据库调用或Web服务类等,而并非纯粹是逻辑上的计算流程)。 This seems to work fine and is my current way of doing things. 这似乎工作正常,并且是我当前的处理方式。 I was wondering if the Main method was even possible/recommended. 我想知道Main方法是否甚至可能/推荐。

Question : Is this even possible with C#/VS or not recommended? 问题 :使用C#/ VS甚至可以做到吗?还是不建议这样做?

Update: I can't add a console project just to achieve this since the addition of projects is tightly controlled by the source control team. 更新:我不能仅添加控制台项目来实现此目的,因为添加项目由源代码管理团队严格控制。 Hence the question of the Main method 'hack' for quick and dirty checks/tests. 因此, Main方法'hack'的问题是快速而肮脏的检查/测试。

Your project type needs to be Console Application for it to "recognize" a Program.Main method, not Class Library. 您的项目类型必须是“控制台应用程序”,才能“识别” Program.Main方法,而不是“类库”。 The intent is for a Class Library to be an encapsulated grouping of functionality that can only be accessed by a project that is set up to allow for user input. 目的是使类库成为功能的封装组,只能由设置为允许用户输入的项目访问。 Those can be a Console Application, Web project (MVC/API), or Desktop (WPF). 这些可以是控制台应用程序,Web项目(MVC / API)或桌面(WPF)。

If you just want to execute a test against the code within a Class Library project, you can also create a Unit Test project, add a reference and execute very explicit tests against the functionality you're looking to achieve. 如果您只想对类库项目中的代码执行测试,则还可以创建一个单元测试项目,添加一个引用,并针对要实现的功能执行非常明确的测试。

You can find out the differences between the different project types by examining the .csproj files in your favorite text editor. 您可以通过在您喜欢的文本编辑器中检查.csproj文件来找出不同项目类型之间的差异。

In Visual Studio go New->Project then select Console Application (in Windows\\Classic Desktop in VS2015). 在Visual Studio中,转到“ New->Project然后选择“ Console Application (在VS2015中的Windows \\ Classic Desktop中)。 This gives you a basic console application with... 这为您提供了具有以下功能的基本控制台应用程序:

static void Main(string[] args)
{
}

setup and ready to go. 设置并准备就绪。 However for simply trying out code you may find this cumbersome (creating a new project and folder just to test code) and for testing code (that doesn't rely on existing libraries) you could use something like .NET Fiddle... 但是对于简单地尝试代码,您可能会发现这很麻烦(创建一个新项目和文件夹只是为了测试代码),而对于测试代码(不依赖现有库),则可以使用.NET Fiddle之类的方法。

https://dotnetfiddle.net/ https://dotnetfiddle.net/

Where you can quickly create and test code there and run it via the browser. 您可以在此处快速创建和测试代码,并通过浏览器运行它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM