Change assemblyInfo from Pre-Build Event MSbuild

DotNET Developper 1 Reputation point
2020-11-30T16:47:07.81+00:00

Hi,
i try to change the assemblyInfo from Pre-build event but i can't access to info added in assemblyInfo.cs. i could change this file by using the WriteCodeFragment in the csproj file but i can't assign a value to itemgroup from pre-build event.bellow you will find my assay in the csproj file.

 <Target Name="AfterBuild">  
    <ItemGroup>  
      <AssemblyAttributes Include="AssemblyTitle">  
        <_Parameter1>MyCompany</_Parameter1>  
      </AssemblyAttributes>  
      <AssemblyAttributes Include="AssemblyDescription">  
        <_Parameter1>My Assembly</_Parameter1>  
      </AssemblyAttributes>  
      <AssemblyAttributes Include="AssemblyCompany">  
        <_Parameter1>Assembly Company</_Parameter1>  
      </AssemblyAttributes>  
      <AssemblyAttributes Include="AssemblyProduct">  
        <_Parameter1>My Product</_Parameter1>  
      </AssemblyAttributes>  
      <AssemblyAttributes Include="AssemblyCopyright">  
        <_Parameter1>Copyright © 2020</_Parameter1>  
      </AssemblyAttributes>  
      <AssemblyAttributes Include="AssemblyCulture">  
        <_Parameter1>  
        </_Parameter1>  
      </AssemblyAttributes>  
      <AssemblyAttributes Include="AssemblyVersion">  
        <_Parameter1>1.0.0.0</_Parameter1>  
      </AssemblyAttributes>  
      <AssemblyAttributes Include="AssemblyFileVersion">  
        <_Parameter1>1.0.0.0</_Parameter1>  
      </AssemblyAttributes>  
      <AssemblyAttributes Include="System.Runtime.InteropServices.Guid">  
        <_Parameter1>154547-548-548778-4547</_Parameter1>  
      </AssemblyAttributes>  
    </ItemGroup>  
    <WriteCodeFragment Language="C#" OutputFile="Properties/AssemblyInfo.cs" AssemblyAttributes="@(AssemblyAttributes)" />  
  </Target>  
 
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,669 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 51,256 Reputation points
    2020-11-30T17:15:15.773+00:00

    I assume you're trying to modify the auto-generated assemblyinfo file in your SDK project. Your Target is configured to run after the build which is far too late. The assemblyinfo file is generated before the build starts so you'd need to hook into the build after it is generated but before the build itself.

    However in your case all you're doing is modifying the assembly metadata. You don't need a build task for that. Set the properties using a standard PropertyGroup. Inside the group set the assembly metadata attributes and the auto-generated assembly info file will pick them up. The directory.build.props file is the ideal place to do this for solution-wide settings. You can configure the properties there and all assemblies will use those settings.

    xml
    <Project>
       <PropertyGroup>
          <Product>My Product</Product>
          <Company>My Company</Company>
       </PropertyGroup>
    </Project>
    

    The only time you'd need to do a custom target is if you wanted to add additional metadata that is not currently supported by the auto-generate task. Again though you can do that using a directory.build.targets file instead. Since this is adding assembly metadata though you should be able to just add it and let it run normally which should be early in the build process. At most you would probably specify a before targets of BeforeCompile.

    xml
    <Target BeforeTargets="BeforeCompile">
    </Target>
    

  2. Dylan Zhu-MSFT 6,406 Reputation points
    2020-12-01T02:17:53.457+00:00

    Hi NETDevelopper-8814,

    The "TargetName" is just a name for your target. As cooldadtx says, you need to specify a separate order in which target is executed. Please refer to this document: Target build order

    <Target Name="AfterBuild" BeforeTargets="build">  
    

    Update:

    If you want to modify csproj file in prebuild event, I'm afraid that it is supported. Please refer to this thread: "Pre-build event is defined in project, when run pre-build command, the project file is already in cache, cannot modify the file.".

    However, there are other tools may help you do this:

    • using ps script to modify the assembly file, please refer to this thread: https://stackoverflow.com/questions/57666790/how-to-programatically-update-assemblyinfo-cs-as-a-pre-build-event-visual-studi
    • using msbuild tool. Firstly, you need to create a environment variable for "AssemblyTitle", which is like: <PropertyGroup>
      <Title>MyCompany</Title>
      </PropertyGroup>
      <ItemGroup>
      <AssemblyAttributes Include="AssemblyTitle">
      <_Parameter1>$(Title)</_Parameter1>
      </AssemblyAttributes>
      </ItemGroup>
      Then, open developer command prompt, and type this command: msbuild XXX.csproj /p:Title = NewName . The new value will apply to the latest generated file.

    Best Regards,
    Dylan


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our **documentation to enable e-mail notifications if you want to receive the related email notification for this thread.**

    0 comments No comments