C# 从.csproj内访问solutioninfo.cs和assemblyinfo.cs

C# 从.csproj内访问solutioninfo.cs和assemblyinfo.cs,c#,properties,csproj,assemblyinfo,C#,Properties,Csproj,Assemblyinfo,我需要访问.csproj文件中solutioninfo.cs和assemblyinfo.cs中的一些信息,并将其用作属性 使用 // my solutioninfo.cs [assembly: AssemblyCompany("MyCompany")] 在我的csproj中: // my .csproj <PublisherName>MyCompany</PublisherName> //my.csproj 我的公司 有没有办法访问这些值?您可以通过反射和A

我需要访问.csproj文件中solutioninfo.cs和assemblyinfo.cs中的一些信息,并将其用作属性

使用

// my solutioninfo.cs    
[assembly: AssemblyCompany("MyCompany")]
在我的csproj中:

// my .csproj
<PublisherName>MyCompany</PublisherName>
//my.csproj
我的公司

有没有办法访问这些值?

您可以通过反射和AssemblyAttribute类来实现这一点。例如:

AssemblyCompanyAttribute company =
 (AssemblyCompanyAttribute)AssemblyCompanyAttribute.GetCustomAttribute(System.Reflection.Assembly.GetExecutingAssembly() , typeof
    (AssemblyCompanyAttribute));

Console.Write(company.Company);

您可能需要添加一个使用系统。反射;指令到代码顶部。

您可以通过反射和AssemblyAttribute类来执行此操作。例如:

AssemblyCompanyAttribute company =
 (AssemblyCompanyAttribute)AssemblyCompanyAttribute.GetCustomAttribute(System.Reflection.Assembly.GetExecutingAssembly() , typeof
    (AssemblyCompanyAttribute));

Console.Write(company.Company);

您可能需要添加一个使用系统。反射;指令到代码顶部。

在csproj文件的最后,有两个空的MSBuild目标,分别称为BeforeBuild和AfterBuild。这两个目标或多或少是对构建前和构建后事件的替代。您可以在那里添加自己的脚本。例如,在从subversion获取版本后,我正在SolutionInfo.cs中设置版本,这是通过使用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>


在csproj文件的最后,有两个空的MSBuild目标,分别称为BeforeBuild和AfterBuild。这两个目标或多或少是对构建前和构建后事件的替代。您可以在那里添加自己的脚本。例如,在从subversion获取版本后,我正在SolutionInfo.cs中设置版本,这是通过使用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>


My.csproj文件是一个xml配置文件。-->无反射My.csproj文件是xml配置文件。-->没有反映可能我的描述不太清楚:我需要*info.cs文件中的信息。我不想更新它们。我想使用.csproj文件中的信息。可能我的描述不太清楚:我需要*info.cs文件中的信息。我不想更新它们。我想使用.csproj文件中的信息。我建议使用这种方法,您可以通过命令行将属性提供给构建,或者直接将它们添加到项目中。然后使用预构建更新*.cs文件,以确定包含该信息的内容。我认为这比反过来要容易得多。我推荐这种方法,您可以通过命令行将属性提供给构建,或者直接将它们添加到项目中。然后使用预构建更新*.cs文件,以确定包含该信息的内容。我想这比走另一条路要容易得多。