您可以在Visual Studio片段中自动插入名称空间吗?

问题描述:

我有一个为我创建C#类的代码段。

I have a code snippet that creates a C# class for me. It puts the regions in all the right places and sets it up just the way I like it.

当我通过添加新项目创建类时,它会自动创建名称空间

When I create a class by adding a new item it automatically creates the namespace based on my project and folder structure.

有没有一种方法可以对我的自定义代码段执行相同的操作?

Is there a way I could do the same action with my custom snippet?

有没有办法将默认类更改为所需的类格式?

Is there a way I can change the default class to look like the class format I want?

我不这样做认为使用VS片段可以做到这一点,只有可用功能很少,它们在MSDN上列出。不过,您可以创建一个新的项目模板,并使用 parameter $ rootnamespace $,它将替换为当前项目的根名称空间。项目模板是Visual Studio的一个非常有用的部分,MSDN具有有关它的大量文档,并且Visual Studio Magazine的很好的介绍a>关于他们。

I do not think that this is possible with VS Snippets, there are only a few available functions, and they are listed at MSDN. You could, though, create a new Item Template and use the parameter $rootnamespace$, which will be replaced with the root namespace of the current project. Item templates are a really useful part of Visual Studio, and MSDN has extensive documentation on it and Visual Studio Magazine had a nice walkthrough about them.