0

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
  • Maybe my descrition was not that clear: I need the info from the *info.cs files. I do not want to update them. I want to use the info within my .csproj file. Commented Sep 10, 2009 at 15:06

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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.

1 Comment

My .csproj file is a xml config file. --> no reflections

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.