alelom
alelom

Reputation: 3008

Visual Studio: Assembly name and Default namespace from project name

Context: setting up the Assembly Name/Default namespace in the Project Properties (right click on project, Properties, Application tab; or edit the .csproj).

I would like to add a parameter there to explicitly have them following the Project Name, plus some custom suffix. So if I rename my project, the Assembly name/namespace follow that rename.

Example: project named foofoo_suffix.dll. If I rename the project to foo1foo1_suffix.dll. Same behaviour for the namespace.

Reading Common macros for MSBuild commands and properties, I tried inputting $(ProjectName) both in VS and in the .csproj file, but I get a .dll named exactly like that.

How can I get this done?

Upvotes: 0

Views: 4497

Answers (1)

LoLance
LoLance

Reputation: 28176

How can I get this done?

1. Try using $(MSBuildProjectName) property from this document.

You're developing a C# project(csproj) while your document above is about macros in C++ projects(vcxproj). So I think $(MSBuildProjectName) property can be more suitable for you after my several tests.

(I tried $(ProjectName), but it only worked well when I set something like <AssemblyName>$(ProjectName)</AssemblyName>, if I add a suffix like $(ProjectName)_suffix, the output sometimes is _suffix.dll. But it always work well if I use $(MSBuildProjectName)!)

I get the desired behaviour, but that is inconsistent with what I can see in project, Properties, Application tab.

2. Try reloading the project file(Right-click project, unload.., reload...) to Update the UI.

From your comment, it seems like the $(ProjectName)_suffix works well in your machine? If so, you can still use $(ProjectName) property.

What you experienced is expected behavior for Msbuild and VS. VS will read value from csproj during project loading process. Then it display the related value by UI.

So after we rename the project name and then build the project, we can get a ProjectName_suffix.dll in output folder. But the UI is still not changed(inconsistent), cause now we need to Update the UI.

You can right-click project node, unload the project=>reload the project to get UI updated. And now the output xx.dll can be consistent with what you see in project, Properties, Application tab. It needs us to reload the project each time after you rename the project, then the UI would be consistent with your xx.dll. And actually it won't affect your build though the UI is not updated, the output can be your desired behaviour.

3. All above is more related to the relationship between Assembly Name and Project Name. One point you should know is msbuild(csproj) won't modify the source files(xx.cs) after the project is created.

So assuming you have a Class1.cs file whose namespace is Library. If you rename the project to NewLibrary, reload the project and you can see the Namespace is NewLibrary now in project, Properties, Application tab. But the namespace for Class1.cs file is still old Library. This is expected behavior in VS, and the new NameSpace NewLibrary would work if you create a new xx.cs file.

Hope all above helps :)

Upvotes: 2

Related Questions