1

I am working on an automated c# build that requires me to write/generate the csproj file and then compile it using the command line. For some reason while the dll is created without issue, the class it contains is dumped into the global namespace instead of the one I have specified in . Does anyone have any idea what might be going on here?

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <OutputType>Library</OutputType>
        <RootNamespace>SimpleDependency.Test</RootNamespace>
        <AssemblyName>simpledependency.test</AssemblyName>
        <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>
    </PropertyGroup>
    <ItemGroup>
        <Compile Include="*.cs" />
    </ItemGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>Bin\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
    </PropertyGroup>
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

I have msbuild set to .net 4.0, and am running this command on the command line:

msbuild /property:Configuration=Release /property:Platform=AnyCPU

I know that it generates the dll successfully because I then have another dependent project that uses the class I have defined in this project, but if I include:

using SimpleDependency.Test;

in that code, I get compile errors saying it cannot find namespace 'SimpleDependency'. Without this using statement, it compiles fine and works. Anyone have any thoughts?

Deleter
  • 11
  • 2
  • Are your classes defined with the SimpleDependency.Test namespace? Defining a RootNamespace will not enforce it in all classes without namespaces. – Pierre-Luc Pineault Sep 24 '14 at 21:21
  • That is the entire point of defining a root namespace is to not have to write it in all your classes – Deleter Sep 25 '14 at 17:30
  • sorry that was supposed to be a question, is that not the point of root namespace? – Deleter Sep 25 '14 at 17:55
  • Well I don't think it'll overwrite .cs files already written without any explicit namespaces. More details in [this question](http://stackoverflow.com/questions/2260038/how-to-force-a-c-sharp-root-namespace-throughout-project-files-for-when-none-is). – Pierre-Luc Pineault Sep 25 '14 at 17:57
  • Gotcha. I guess when you set the root namespace, visual studio must just inject it in every new class you create. – Deleter Sep 25 '14 at 18:50

2 Answers2

1

Run MSBuild using the /preprocess:flattened.proj flag. Then load up the resulting file in an XML editor. My recent experience is that when properties are not being seen, it's overwritten someplace later (e.g. setting rather than appending to it) or something about conditions. That's a good start. You might also try getting MSBuildExplorer3 and see if that turns up anything. I'm not familiar with C# projects, but I think you should find where $(RootNamespace) is actually used for its effect, and trace backwards: is it ignored due to a condition, not getting the target variation you expected, etc. Once you know the lay of the land, run MSBuild with /verbosity:diag and grep through that for the target where it's (supposed to be) used, and see what it was thinking.

JDługosz
  • 5,592
  • 3
  • 24
  • 45
  • Thanks, turned out I had another problem, but these techniques (msbuildexplorer 3 and preprocess:) will surely help me sort more information out when I have other problems in the future. – Deleter Sep 25 '14 at 18:53
  • Yea, when I was figuring this out, knowing how to find out what was going on was the main enabler. – JDługosz Sep 26 '14 at 00:08
0

Copying the feedback from Pierre-Luc into an answer: The rootnamespace appears to only be a suggestion to the IDE to inject whenever creating classes. If the .cs files do not have a namespace specified, rootnamespace will not become the namespace for those classes. More information about that problem in this question.

Community
  • 1
  • 1
Deleter
  • 11
  • 2