我需要从我的 .csproj 文件中的 solutioninfo.cs 和 assemblyinfo.cs 访问一些信息并将其用作属性。
使用的价值
// my solutioninfo.cs
[assembly: AssemblyCompany("MyCompany")]
在我的 csproj 中:
// my .csproj
<PublisherName>MyCompany</PublisherName>
有没有办法访问这些值?
我需要从我的 .csproj 文件中的 solutioninfo.cs 和 assemblyinfo.cs 访问一些信息并将其用作属性。
使用的价值
// my solutioninfo.cs
[assembly: AssemblyCompany("MyCompany")]
在我的 csproj 中:
// my .csproj
<PublisherName>MyCompany</PublisherName>
有没有办法访问这些值?
在您的 csproj 文件的最后,有两个名为 BeforeBuild 和 AfterBuild 的空 MSBuild 目标。这两个目标或多或少是对构建前和构建后事件的替代。您可以在那里添加自己的脚本。例如,在从 subversion 获取它之后,我在 SolutionInfo.cs 中设置版本,这是通过使用 MSBuild.CommunityTasks 完成的:
<Target Name="BeforeBuild">
<FileUpdate
Files="$(SolutionInfoFile)"
Regex="(?<ver>assembly: AssemblyVersion\(").*""
ReplacementText="${ver}$(Major).$(Minor).$(Build).$(Revision)"" />
<FileUpdate
Files="$(SolutionInfoFile)"
Regex="(?<ver>assembly: AssemblyFileVersion\(").*""
ReplacementText="${ver}$(Major).$(Minor).$(Build).$(Revision)"" />
<FileUpdate
Files="$(SolutionInfoFile)"
Regex="(?<ver>assembly: AssemblyInformationalVersion\(").*""
ReplacementText="${ver}$(Major).$(Minor).$(Build)"" />
</Target>
AFAIR 带有正则表达式的 FileUpdate 任务也是 CommunityTasks 的一部分。
您可以通过 Reflection 和 AssemblyAttribute 类来做到这一点。例如:
AssemblyCompanyAttribute company =
(AssemblyCompanyAttribute)AssemblyCompanyAttribute.GetCustomAttribute(System.Reflection.Assembly.GetExecutingAssembly() , typeof
(AssemblyCompanyAttribute));
Console.Write(company.Company);
您可能需要添加一个 using System.Reflection; 指令到代码的顶部。