简体   繁体   中英

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

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

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

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.

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.

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.

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; 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. 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.

additionaly for git bash use 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. Today I found a replacement extension for the new class/interface functionality called "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. 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. ...

I searched and found that we can install these templates using command

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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