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

心不动则不痛 提交于 2021-02-06 14:24:20

问题


I'm learning how to use dotnet-cli with 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.

Thanks for helping


回答1:


Not that I know of (I was also researching that topic) but I found helpful extension for VS code called C# Extensions by jchannon, which helps you create classes and interfaces with correct namespace by right-clicking the folder in VS code explorer and selecting to create C# class or C# interface.




回答2:


If you’re after plain old C# classes then yeah, I’m not aware of cli support. However, VS Code can create classes with the “right” namespaces by right-clicking on a folder and selecting Add new class (or somesuch).

However the dotnet aspnet-codegenerator can create boilerplate classes for various elements of an MVC application. Here is a discussion.




回答3:


Just add the file in the same directory or sub-directory then build the project using Core CLI, it will be added. 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.




回答4:


Visual Studio has an UX that allows you to right-click a project or a folder underneath a project and open a context window. 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. These item templates are backed by .vstemplate files and complemented by a file of that type. For example, for a C# class, there is a Class folder with a Class.cs file and a Class.vstemplate file. 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. This is a starting point.

According to Tutorial: Create a project template for dotnet new,

With .NET Core, you can create and deploy templates that generate projects, files, even resources. 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. [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.

Based on this information, my next step would be to try and create some dotnet new file templates. 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.



来源:https://stackoverflow.com/questions/49087421/how-to-add-a-new-c-sharp-file-to-a-project-using-dotnet-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!