簡體   English   中英

如何動態更新Assemblyinfo.cs中的值

[英]How to update the Value in Assemblyinfo.cs dynamically

我編寫了一個從SVN存儲庫中獲取值的程序。 現在我想用該值更新AssemblyFileversion。

由於我無法在Assemblyinfo.cs中編寫任何代碼,我將如何更新AssemblyFileVersion的值。

我希望實現這樣的目標

..........................
// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

 SvnInfoEventArgs info;
                    Uri repoURI = new Uri("ServerAddress");
                    svnClient.GetInfo(repoURI, out info);


[assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion(String.Format("{0}.{1}.{2}. {3}",
                                          major,minor,build,info.Revision))]

我假設你試圖在這里生成一些構建系統 - 所以基本上,你需要修改帶有svn編號的AssemblyInfo.cs文件。

您可以為此創建MS Build任務:請參閱http://florent.clairambault.fr/insert-svn-version-and-build-number-in-your-c-assemblyinfo-file

有命令行實用程序(SubWCRev)也可用於基於關鍵字的替換($ WCREV $用於修訂) - 請參閱此處的示例: http ://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev-example 。 HTML

最后,您可以推出自定義代碼(進行正則表達式搜索/替換)以執行與某些(包括我)相同的操作 - 請參閱此處的一個示例: http//www.codeproject.com/Articles/168528/自動保持與程序集修訂同步

我正在使用基於ant的構建系統,並且有關於更新AssemblyInfo.cs的類似問題。

我的構建系統已經生成了在構建時包含在其他項目中的version.h文件。 對於C#,我決定使用生成的VersionInfo.cs將版本信息注入AssemblyInfo.cs

AssemblyInfo.cs (設置一次指定VersionInfo常量)

using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyCompany(VersionInfo.Company)]
[assembly: AssemblyProduct(VersionInfo.ProductName)]
[assembly: AssemblyCopyright(VersionInfo.Copyright)]

[assembly: Guid("12345678-1234-1234-1234-1234567890ab")]

[assembly: AssemblyVersion(VersionInfo.product.FileVersion)]
[assembly: AssemblyFileVersion(VersionInfo.sdk.FileVersion)]

VersionInfo.cs (由ant構建系統動態生成)

// Shared Product Version Header
// Automatically Generated: Sat, 2 May 2015 15:09:09 EDT
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
internal struct VersionInfo
{
  public const string Company = @"My Company Corp";
  public const string Copyright = @"Copyright (c) 2015 My Company Corp.  All rights reserved.";
  public const string ProductName = @"My Software Service";
  public const string SpecialBuild = @"";
  public const string ProductConfiguration = @"";
  public const string ProductCode = @"MSS";
  public const string ProductUrl = @"www.MyCompany.com";
  public const string ProductEmail = @"support@MyCompany.com";
  public const string ProductVendor = @"My Company Corp";
  internal struct product
  {
    public const string FileVersion = @"1.5.0.240";
  }
  internal struct sdk
  {
    public const string FileVersion = @"1.4.5.222";
  }
  internal struct service
  {
    public const string FileVersion = @"1.3.2.142";
  }
}

version.xml (ant import - snipped)

<echo file="${artifacts.dir}\VersionInfo.cs" append="false">// Shared Product Version Header${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">// Automatically Generated: ${version.generated.time}${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">// ReSharper disable CheckNamespace${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">// ReSharper disable InconsistentNaming${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">internal struct VersionInfo${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">{${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">  public const string Company = @"${product.company}";${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">  public const string Copyright = @"${product.copyright}";${line.separator}</echo>
 <!-- other version info here -->
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">}${line.separator}</echo>

version.properties (指定產品版本信息的ant屬性文件)

product.company=My Company Corp
product.copyright=Copyright (c) 2015 My Company Corp.  All rights reserved.
product.website=www.MyCompany.com
product.email=support@MyCompany.com
product.vendor=My Company Corp

product=1.5.0.240
service=1.3.2.142
sdk=1.4.5.222

C#預構建事件 (將自動生成的VersionInfo.cs復制到項目屬性目錄)

@ECHO OFF
SETLOCAL
SET ARTDIR=$(ProjectDir)..\..\..\MySoftwareService\installer
SET VERCS=%ARTDIR%\VersionInfo.cs
ECHO Updating %VERCS%
xcopy /D /Y "%VERCS%" "$(ProjectDir)Properties"

MSBuild擴展 (沒有!)

<!-- none, not required -->

順便說一句,我的版本方案是:major.minor.revision.build,它不是AssemblyInfo.cs文件指定的。

您需要在構建過程中執行此操作。

由於您使用的是Visual Studio,因此您已經在使用MSBuild ,在這種情況下, MSBuild擴展包具有AssemblyInfo任務。

為了從Subversion獲得修訂版,MSBuild擴展包也有一個任務

或者,如果您使用TeamCity進行持續集成(CI),則它具有“AssemblyInfo修補程序”構建功能。 我猜大多數其他CI系統都會有類似的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM