Zach Zach - 1 year ago 68
C# Question

Custom template namespace error

My custom project template uses Sidewaffle in Visual Studio. It works fine until I try to add a new class to the existing project.

Whenever a new class is added, the namespace is the same as the one in the project I used to create the template - as opposed to the one for the current project. The template replaces the namespace in the default files, just not the new ones.

Can I fix this or do I have to change the namespace each time?

Answer Source

So I found a workaround to the error. First I followed the steps outlined here: Proper way to rename solution (and directories) in Visual Studio. I figured I might have had a problem earlier when I renamed my solution that contained my template file.

That still didn't fix my problem because when looking at the default namespace under the properties section of the project it was the namespace in the project I used to create the template (as expected). However, Sidewaffle is supposed to take the namespaces and replace them with "$safeprojectname$". This doesn't seem to work for the default namespace, so whenever a new file is added, the incorrect namespace will be used.

Visual Studio doesn't let you enter "$safeprojectname$" for the default namespace because it says it's an invalid format. Long story short, you need to edit the .csproj file of the project that the template is modeled after (using any text editor)

`<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="">
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>

and change the "RootNamespace" property to "$safeprojectname$". I also changed the "AssemblyName" to "$safeprojectname$" for good measure. Save the file, and you should be good.