简体   繁体   English

c#语法检查

[英]c# syntax checking

I create a project, and add a file.我创建了一个项目,并添加了一个文件。 I can build an exe and so on, works fine.我可以构建一个exe等,工作正常。

Question is: what if I have multiple files in my project and I want to check the syntax of my code in one single file, and don't want to build an exe or dll?问题是:如果我的项目中有多个文件并且我想在一个文件中检查我的代码的语法,并且不想构建 exe 或 dll 怎么办?

When programming c, I press ctrl+F7 in the Visual Studio IDE and that compiles my code.编程 c 时,我在 Visual Studio IDE 中按 ctrl+F7 并编译我的代码。 I understand that there are no object files in c#.我知道 c# 中没有目标文件。 Microsoft c# reference states: "There are no object (.obj) files created as a result of invoking the C# compiler; output files are created directly.". Microsoft c# 参考声明:“没有因调用 C# 编译器而创建的对象 (.obj) 文件;直接创建输出文件。”。

Is there a csc.exe switch to tell me 'ok your code looks fine', or 'the expression at line xx doesn't make sense'?是否有一个 csc.exe 开关来告诉我“好吧,你的代码看起来不错”,或者“第 xx 行的表达式没有意义”?

Note: please do not give the link to Command-line Building With csc.exe.注意:请不要提供使用 csc.exe 构建命令行的链接。 All the sample command lines are for creating an exe or dll, from one or multiple files.所有示例命令行都用于从一个或多个文件创建 exe 或 dll。 Or on how to exclude a file from building.或者关于如何从构建中排除文件。 I don't want that.我不想要那个。

The compiler is the syntax checker.编译器是语法检查器。 It will tell you if your C# code is valid or not when it compiles it.它会在编译时告诉您 C# 代码是否有效。 Any errors or warnings will be available at the end of the attempted compilation.在尝试编译结束时,任何错误或警告都将可用。 If there are none, then it's valid code.如果没有,那么它是有效的代码。

You can individually compile any arbitrary piece of C# code any time you like.您可以随时单独编译任意一段 C# 代码。 However , if that code depends on other code then in order for it to be valid that other code will need to be included in the compilation as well.但是,如果该代码依赖于其他代码,那么为了使其有效,其他代码也需要包含在编译中。 Code which uses otherwise undefined symbols (such as classes defined in other code files) isn't valid.使用其他未定义符号的代码(例如在其他代码文件中定义的类)是无效的。 Those symbols need to be defined.需要定义这些符号。

In the comments above you indicate a concern that such compilation of an entire project might take a long time.在上面的评论中,您表示担心整个项目的这种编译可能需要很长时间。 This would be addressed by organizing your code into smaller components.这可以通过将您的代码组织成更小的组件来解决。 If you have one enormous C# project that takes a long time to compile, then what you have is a mess.如果您有一个需要很长时间编译的庞大 C# 项目,那么您所拥有的就是一团糟。 Break it apart into smaller components.将其分解为更小的组件。 Each of those components can be in their own projects which can be compiled separately.这些组件中的每一个都可以在它们自己的项目中,这些项目可以单独编译。 Different projects will depend on each other, and dependent projects will be included in that compilation.不同的项目将相互依赖,并且依赖项目将包含在该编译中。 But that dependency graph also shouldn't be too large and unwieldy.但是依赖关系图也不应该太大和笨拙。 If it is, you still have a mess but just on a different scale.如果是这样,您仍然一团糟,只是规模不同。 Keep the dependency graph shallow and maintainable between your projects.在你的项目之间保持依赖图的浅层和可维护性。

Sort of answering my own question.有点回答我自己的问题。 When I posted this I wasn't aware that splitting definition and implementation in c#, as you did in c or c++, was not possible.当我发布这篇文章时,我不知道在 c# 中拆分定义和实现,就像你在 c 或 c++ 中所做的那样,是不可能的。 https://social.msdn.microsoft.com/Forums/en-US/64800602-4ff2-4747-8c42-a4e1980d4124/c-no-header-files-all-code-is-written-inline-why?forum=csharplanguage states that Interfaces do this, but not in a way I asked in the original post. https://social.msdn.microsoft.com/Forums/en-US/64800602-4ff2-4747-8c42-a4e1980d4124/c-no-header-files-all-code-is-written-inline-why?forum= csharplanguage声明接口这样做,但不是我在原始帖子中问的方式。 Looks like in a multi file project, only the IDE gives me this piece of information (syntax checking), other than building the whole project.看起来在一个多文件项目中,除了构建整个项目之外,只有 IDE 给了我这条信息(语法检查)。

It appears that ctrl+F7 on a single file (Visual Studio), or -c or -fsyntax-only switches (on gcc) are not possible with c#. c# 似乎无法在单个文件(Visual Studio)上使用 ctrl+F7 或 -c 或 -fsyntax-only 开关(在 gcc 上)。

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

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