简体   繁体   中英

Access solutioninfo.cs and assemblyinfo.cs from within .csproj

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM