Can you automatically insert a namespace in a visual studio snippet?
Asked Answered
J

2

7

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?

Jillane answered 4/5, 2009 at 14:12 Comment(1)
Given the new developments it would be appropriate to change the accepted answer for future readers like myself.Liddie
R
4

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.

Rancell answered 5/5, 2009 at 17:25 Comment(0)
M
10

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

<Snippet>
  ...

  <Imports>
    <Import>
      <Namespace>SomeNamespace</Namespace>
    </Import>
    <Import>
      <Namespace>AnotherNamespace</Namespace>
    </Import>
  </Imports>
</Snippet>

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

Moonstone answered 24/9, 2018 at 19:14 Comment(1)
Worth noting it is to be included inside your <Snippet> node. Add an <Import> node for each namespace you need.Muscarine
R
4

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.

Rancell answered 5/5, 2009 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.