Can you automatically insert a namespace in a visual studio snippet?

亡梦爱人 提交于 2019-12-10 15:59:27

问题


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?


回答1:


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.




回答2:


This is possible now (2018), at least part of your's wishes, with adding this:

<Imports>
  <Import>
    <Namespace>YourNameSpace</Namespace>
  </Import>
</Imports>

for more info: https://github.com/dotnet/roslyn/issues/4457

(this answer is obviously for new explorers)



来源:https://stackoverflow.com/questions/820286/can-you-automatically-insert-a-namespace-in-a-visual-studio-snippet

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