简体   繁体   English

是否可以在编译时获取汇编信息而无需进行反思?

[英]Is it possible to get assembly info at compile time without reflection?

It is trivial to get Assembly information at run-time using reflection: 使用反射在运行时获取程序集信息很简单:

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);

However, I cannot use reflection in my project due to restricted execution environment. 但是,由于执行环境受限制,我无法在项目中使用反射。 In limited trust environment this code will not work. 在有限的信任环境中,此代码将不起作用。

I want to use some attributes from AssemblyInfo.cs at compile time to show product name, version etc. without invoking reflection mechanism. 我想在编译时使用AssemblyInfo.cs一些属性来显示产品名称,版本等,而无需调用反射机制。

Most primitive way would be just duplicating strings from AssemblyInfo.cs file as constant strings somewhere else. 最原始的方法是将AssemblyInfo.cs文件中的字符串复制为其他地方的常量字符串。 But maybe there exist some more elegant solutions? 但是也许存在一些更优雅的解决方案?

Of course, some essential info on assembly can be properly resolved only at run-time through reflection. 当然,只有在运行时通过反射才能正确解决一些有关组装的基本信息。 Say, assembly path, execution context etc. But info about name and version is written in project code and should be normally available at compile time. 说,汇编路径,执行上下文等。但是有关名称和版本的信息用项目代码编写,通常应在编译时使用。

Any comments and ideas are appreciated. 任何意见和想法表示赞赏。 Thank you. 谢谢。

Since nobody posted an answer so far, I will publish a variant, to which I came based on comments to the question. 由于到目前为止还没有人发布答案,因此我将发布一个变体,我根据对该问题的评论来得出该变体。 As @Julo advised, I took way exactly opposite to getting info from attributes and, instead, decided for constant definitions in a class: 正如@Julo所建议的,我采取了与从属性获取信息完全相反的方法,而是决定在类中定义常量:

public class Resource
{
    public const string VERSION = "1.0.0.0";
    public const string COMPANY = "Company";
    public const string APPICATION = "App";
}

Then I pass these values to assembly attributes: 然后,将这些值传递给程序集属性:

[assembly: AssemblyTitle(Resource.APPICATION)]
[assembly: AssemblyCompany(Resource.COMPANY)]
[assembly: AssemblyVersion(Resource.VERSION)]
[assembly: AssemblyFileVersion(Resource.VERSION)]

Of course, I can also access these in my code at compile time: 当然,我也可以在编译时在代码中访问它们:

string tool = null, version = null;
//System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
//System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
//if (string.IsNullOrEmpty(tool) && fvi != null) tool = fvi.ProductName;
//if (string.IsNullOrEmpty(version) && fvi != null) version = fvi.FileVersion;
tool = Resource.APPICATION;
version = Resource.VERSION;

Drawback here can be that automatic version increment tools in build environments will not be able to access these constants. 缺点是构建环境中的自动版本增量工具将无法访问这些常量。 Advantage can be that constants can be shared between many projects as a single file to enable cetralised version management for the whole solution. 优点是常量可以作为单个文件在多个项目之间共享,从而可以对整个解决方案进行集中式版本管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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