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

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?

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

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.

Related

How to create a VisualStudio item template that does not offer numbered file names by default

If I create an item template with default name foo.txt, Visual Studio by default offers the name foo1.txt in the add new dialog, even if there is no foo.txt in the folder.
This is normally fine, but I would need to create an item template, that does not offer a "1" suffix for the file name by default, exactly like the App.config template behaves.
I have checked the App.vstemplate file in the VS2019 installation, but I don't see any special setting that would cause this behavior.
Sample:
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<DefaultName>foo.txt</DefaultName>
<Name>Foo.txt file</Name>
<Description>A foo.txt.</Description>
<ProjectType>CSharp</ProjectType>
</TemplateData>
<TemplateContent>
<ProjectItem>foo.txt</ProjectItem>
</TemplateContent>
</VSTemplate>
Shows in the add new dialog as:
Sorry but I'm afraid the answer would be negative. I think this
behavior is by design and can't be controlled by any Element in the
.vstemplate.
I've reproduced this issue in my side. And one interesting thing I found is if we change the file extensions to .manifest and this issue goes away.
For example:
If the Item template I want to create is something like a foo.manifest instead of foo.txt, then when I add this item in new project it won't display the 1 suffix. You can easily confirm this point by change all your foo.txt content to foo.manifest.
I agree with Will that they're special casing some of the templates. And to be more specific, this behavior is special for the file extensions of the Item we created. I haven't done anything differently when creating xxx.manifest Item, but it just makes a difference from the xxx.txt Item. So I would think there is a invisible and unknown rule to control this behavior, and this behavior may not be changed or controlled in custom extension. It would be something like a rule designed by the Product Team I believe.
In my opinion, it's not supported by current VS SDK to control the behavior to let VS won't display '1' suffix for item whose file extension is .txt. And if you do want this feature, go Developer Community to share your great idea there. Actually after your reminder, it would be better if this feature comes then we can really control the Default Name, so if you decide to post your idea, share the link in your question and members interested in it would help vote for it.
Hope it helps and if i misunderstand anything, feel free to let me know:)

Visual Studio DSL interaction with my classes

I'm getting start with DSL, I have build my DSL and I have a T4 template for generate code from a design.
I can create a new element in my project of my DSL type, I can create my design and then I have to run my T4 to generate code.
But, I can see how to work the default Visual Studio Class Diagram, it don't need to run any T4. For example when you drop a class in the diagram it shows a dialog to choose the class name and file.
When I change a class (adding properties for example) by edit the file the diagram update itself the new information (properties, methods, ...). When you add a property in a class using the diagram its update the file adding the correct property ...
How to do it? I'm newbie and I would like to find documentation about it.

Finding T4 text template class code

T4 text templates can be used to generate not only code but also any kind of text with visual studio.
I've read blogs and tutorials about T4 and as far as I can understand, visual studio dynamically builds a class in the background, compiles and runs the code in that class to build the text output.
Is it possible to see the source code of that class?
Yes, the easiest way is to change the Custom Tool in the properties window when the template file is selected in Solution Explorer.
By default, it will be 'TextTemplatingFileGenerator'.
If you change the custom tool to 'TextTemplatingFilePreprocessor' you'll get the underlying template class instead of the template output generated into your project.
To be precise, this code won't be exactly the same as that which is run under the covers, but it will be very close.
If you need the absolute exact code, you should leave the custom tool alone, but set the debug="true" flag on your <## template #> directive. This will then leave the generated code sitting around in a random named '.cs' or 'vb' file in your %TEMP% directory. Just sort the directory by time and it should be up at the top.

Visual studio 2010 - Class diagram with namespaces

How can I create class in specific folder (namespace) in my project, and see the classes grouped by there namespace?
1) Create a class in the appropriate folder with the option Add new Item.
2) Drop the class in to the class diagram.
http://social.msdn.microsoft.com/Forums/en-US/vsclassdesigner/thread/bebfa1a4-1930-4a56-b0fc-e15a238650ac/
When using Visual Studio Ultimate you can choose "Architecture" from the menu and generate a namespace diagram.
Once the diagram has rendered, you can expand the namespaces and other elements to show the classes they contain.
Note, specific folder != namespace. New files in given folders are defaulted to the namespace, but from experience, try not to rely on it when working in a team or with someone elses' code. ;-)

How do I add Same Solution Project References when exporting Templates in Visual Studio?

I have a solution that contains several projects, lets call them ProjectA and ProjectB. I need ProjectA to have a reference to ProjectB, however when I "Export Template" I can't seem to keep the reference. What I am trying to do is to have it create the reference for me when I create a new project and then automatically name it to what I entered.
This is slightly out of scope for pure Visual Studio templates.
Visual Studio templates supports replacements parameters for templates, so what you could do is:
Inside your ProjectA.zip template file you will find ProjectA.csproj, which is the template for the project
In ProjectA.csproj you will find an <ItemGroup> containing <Reference> entries
Among them should be an entry like <Reference Include="ProjectB">
When you create a project from your ProjectB-template, it's name will be user-specified, so you want to replace ProjectB with a replacement parameter like $Foo$.
This is where it gets out of scope for pure templates: How do you tell visual studio to replace $Foo$? And what would the replacement be?
Your only choice here it to use a custom wizard, which either looks for the ProjectB-type project or asks the user for it.
Here's an outline for how to get the template and custom wizard to get along:
Create a VSIX package project.
Be sure to add the [ProvideBindingPath] attribute to your package. This is required to get the custom wizard registered (otherwise you would need to install it to the GAC, so VS can find it)
Create your wizard, it should implement Microsoft.VisualStudio.TemplateWizard.IWizard
Add your template to the solution and add it as a ProjectTemplate asset in your package's vsixmanifest
In the .vstemplate file of your template, add the following snippet below the <TemplateContent> block:
<WizardExtension>
<Assembly>Your.Packages.Assembly</Assembly>
<FullClassName>Your.Wizard</FullClassName>
</WizardExtension>
That will bring up your wizard when the project is created. You could, in the RunStarted method bring up a dialog which asks for the reference or you could use the automationObject parameter (which actually is a EnvDTE.DTE instance) to try and find the ProjectB project and supply it's name in the replacementsDictionary.

Resources