views:

431

answers:

2

I need to access some information from my solutioninfo.cs and assemblyinfo.cs within my .csproj file and use it as a property.

use the value of

// my solutioninfo.cs    
[assembly: AssemblyCompany("MyCompany")]

in my csproj:

// my .csproj
<PublisherName>MyCompany</PublisherName>

Is there a way to access the values?

+1  A: 

You can do this via Reflection and the AssemblyAttribute classes. For example:

AssemblyCompanyAttribute company =
 (AssemblyCompanyAttribute)AssemblyCompanyAttribute.GetCustomAttribute(System.Reflection.Assembly.GetExecutingAssembly() , typeof
    (AssemblyCompanyAttribute));

Console.Write(company.Company);

You may need to add a using System.Reflection; directive to the top of your code.

Ash
My .csproj file is a xml config file. --> no reflections
crauscher
+2  A: 

At the very end of your csproj file there are two empty MSBuild targets called BeforeBuild and AfterBuild. Those two targets are more or less the replacement of pre- and post-build events. You can add your own script there. I am setting the version in SolutionInfo.cs for instance after getting it from subversion, which is accomplished by using the MSBuild.CommunityTasks:

<Target Name="BeforeBuild">
  <FileUpdate
    Files="$(SolutionInfoFile)"
    Regex="(?&lt;ver&gt;assembly: AssemblyVersion\(&quot;).*&quot;"
    ReplacementText="${ver}$(Major).$(Minor).$(Build).$(Revision)&quot;" />
  <FileUpdate
    Files="$(SolutionInfoFile)"
    Regex="(?&lt;ver&gt;assembly: AssemblyFileVersion\(&quot;).*&quot;"
    ReplacementText="${ver}$(Major).$(Minor).$(Build).$(Revision)&quot;" />
  <FileUpdate
    Files="$(SolutionInfoFile)"
    Regex="(?&lt;ver&gt;assembly: AssemblyInformationalVersion\(&quot;).*&quot;"
    ReplacementText="${ver}$(Major).$(Minor).$(Build)&quot;" />
</Target>

AFAIR the FileUpdate task with the regular expression is also part of CommunityTasks.

Marc Wittke
I would recommend this approach, you can feed properties to the build via the command-line or directly add them to your project. Then use the pre-build to update the *.cs files you what to contain that information. I think this will be a lot easier than going the other way around.
csharptest.net