简体   繁体   English

如何使用 dotnet-cli 将新的 c# 文件添加到项目中

[英]how to add a new c# file to a project using dotnet-cli

I'm learning how to use dotnet-cli with VSCode.我正在学习如何将 dotnet-cli 与 VSCode 一起使用。 I've seen many commands on how to create solution, projects, add reference to projects... but I don't see anywhere in the documentation how to add a file.我已经看到了许多关于如何创建解决方案、项目、添加对项目的引用的命令......但我在文档中的任何地方都没有看到如何添加文件。 If remember, in RubyOnRails it was possible to add file from the command line.如果还记得的话,在 RubyOnRails 中可以从命令行添加文件。

Thanks for helping感谢您的帮助

不是我知道的(我也在研究那个主题),但我发现了一个有用的 VS 代码扩展,名为 jchannon 的 C# Extensions,它可以帮助您通过右键单击 VS 代码资源管理器中的文件夹并选择来创建具有正确命名空间的类和接口创建 C# 类或 C# 接口。

If you're after plain old C# classes then yeah, I'm not aware of cli support.如果您追求的是普通的旧 C# 类,那么是的,我不知道cli支持。 However, VS Code can create classes with the “right” namespaces by right-clicking on a folder and selecting Add new class (or somesuch).但是,VS Code 可以通过右键单击文件夹并选择添加新类(或类似的)来创建具有“正确”命名空间的类。

However the dotnet aspnet-codegenerator can create boilerplate classes for various elements of an MVC application.但是,dotnet aspnet-codegenerator 可以为 MVC 应用程序的各种元素创建样板类。 Here is a discussion.是一个讨论。

Just add the file in the same directory or sub-directory then build the project using Core CLI, it will be added.只需将文件添加到同一目录或子目录中,然后使用 Core CLI 构建项目,它就会被添加。 As per the Core CLI documentation by default all the source files are included in the build, without being added into the project files, though there are option available to override this default behaviour.根据 Core CLI 文档,默认情况下所有源文件都包含在构建中,而不是添加到项目文件中,尽管有可用选项来覆盖此默认行为。

Visual Studio has an UX that allows you to right-click a project or a folder underneath a project and open a context window. Visual Studio 有一个 UX,允许您右键单击项目或项目下的文件夹并打开上下文窗口。 Selecting Add brings up a modal that allows you to select a file template.选择添加会弹出一个允许您选择文件模板的模式。 This intelligently adds the corresponding file to the location specified, adding a calculated namespace and default using statements that are typical for that file type.这会智能地将相应的文件添加到指定的位置,添加计算的命名空间和默认using该文件类型的典型语句。 These item templates are backed by .vstemplate files and complemented by a file of that type.这些项目模板由.vstemplate文件支持,并由该类型的文件补充。 For example, for a C# class, there is a Class folder with a Class.cs file and a Class.vstemplate file.例如,对于 C# 类,有一个 Class 文件夹,其中包含一个 Class.cs 文件和一个 Class.vstemplate 文件。 The former is a template for a C# file, the latter is an XML-based file that describes the template, per the XML namespace described here .前者是 C# 文件的模板,后者是根据此处描述的 XML 命名空间描述模板的基于 XML 的文件。 This is a starting point.这是一个起点。

According to Tutorial: Create a project template for dotnet new ,根据教程:为dotnet new 创建项目模板

With .NET Core, you can create and deploy templates that generate projects, files , even resources.使用 .NET Core,您可以创建和部署生成项目、文件甚至资源的模板。 This tutorial is part two of a series that teaches you how to create, install, and uninstall, templates for use with the dotnet new command.本教程是教您如何创建、安装和卸载模板以用于dotnet new命令的系列教程的第二部分。 [emphasis added] [重点补充]

This blog post, How to create your own template for dotnet new , has some examples how to add replaceable parameters and optional content , which would be good for adding namespaces, class names, class keywords, etc.这篇博文How to create your own template for dotnet new有一些示例如何添加可替换参数可选内容,这对于添加命名空间、类名、类关键字等很有帮助。

Based on this information, my next step would be to try and create some dotnet new file templates.基于这些信息,我下一步将尝试创建一些dotnet new文件模板。 After the basics of creating a file, I would then experiment with how to make them smarter, like these VS Templates, using replaceable parameters and optional content.在创建文件的基础知识之后,我将尝试如何使用可替换参数和可选内容使它们更智能,例如这些 VS 模板。

just type只需键入

new-item YourCSharpFileName.cs

i know this not your point but;我知道这不是你的意思,但是; someone should be putted this basic answer here.应该把这个基本答案放在这里。 this powershell command doesn't create a templated csharp class but;此 powershell 命令不会创建模板化 csharp 类,但是; if you find easier to add a new csharp (or whatever you want. it depends on your filename.extension ) more than add via VS or VS Code like me.如果您发现添加新的 csharp (或任何您想要的。这取决于您的filename.extension )比像我这样通过 VS 或 VS Code 添加更容易。 it could be very helpfull.这可能会很有帮助。 add your file than configure the other inheritence or using things.添加您的文件而不是配置其他继承或使用事物。 it will be same as the vs or vs code generated files.它将与 vs 或 vs 代码生成的文件相同。

additionaly for git bash use touch YourFileName.cs另外对于 git bash 使用touch YourFileName.cs

i hope this help someone.我希望这对某人有所帮助。 if anything wrong please comment me.如果有什么问题请评论我。

I used to use the plugin C# Extensions which is no longer under development.我曾经使用不再开发的插件 C# Extensions。 Today I found a replacement extension for the new class/interface functionality called "C# Stretch": https://marketplace.visualstudio.com/items?itemName=jacokok.csharp-stretch今天,我找到了一个名为“C# Stretch”的新类/接口功能的替代扩展: https ://marketplace.visualstudio.com/items?itemName=jacokok.csharp-stretch

Its a context-menu item in the file explorer allowing you to type in a class name.它是文件资源管理器中的一个上下文菜单项,允许您输入类名。 It produces a C# file with an empty class with the namespace declared in the new file-scoped manner.它生成一个 C# 文件,其中包含一个空类,其命名空间以新的文件范围方式声明。 The nesting namespace format is available as an option in settings.嵌套命名空间格式可作为设置中的一个选项使用。

dotnet new provides many templates like mvc, webapp, classlib, etc. but doesn't provide templates for class , interface , struct , etc. ... dotnet new提供了许多模板,如 mvc、webapp、classlib 等,但不提供classinterfacestruct等模板。

I searched and found that we can install these templates using command我搜索并发现我们可以使用命令安装这些模板

dotnet new -i Yae.Templates::0.0.2 dotnet new -i Yae.Templates::0.0.2

and then you can use command in your preferred directory然后您可以在首选目录中使用命令

dotnet new class -t <nameOfClass_without_extension_.cs> dotnet new class -t <nameOfClass_without_extension_.cs>

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

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