在运行时获取程序集属性的最佳方法 [英] Best way to get assembly attribute at runtime

查看:63
本文介绍了在运行时获取程序集属性的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了(我认为是)非常不寻常的情况.

I've encountered (what I think is) a very unusual scenario.

我们将工具 NetRevisionTool 用作总体项目政策的一部分,这为我们提供了非常有用的元数据,这对我们很有帮助在运行时进行日志记录等等.

We leverage a tool NetRevisionTool as part of our overall project policy, this give us a hugely useful blob of metadata that is helpful at runtime for logging and so on.

挑战是这样-最初我创建了一个简单的静态类,可以调用它,并且在运行时将获取此元数据字符串(基本上该字符串是AssemblyInformationalVersion属性).

The challenge is this - initially I created a simple static class that could be called and at runtime would get this metadata string (basically the string is the AssemblyInformationalVersion attribute).

该类已添加到ASP.NET MVC解决方案中,该解决方案包含多个不同的Web项目,每个Web项目都有一个主页(实际上,这些都是不同的LTI工具,但这并不重要).然后在每个主页中添加如下一行:

That class was added to an ASP.NET MVC solution that contains numeorous distinct web project each of which has a home page (these are in fact distinct LTI tools but that's not important). Then in each home page I added a line like this:

@Html.Hidden(Ncu.Diagnostics.BuildSupport.InstanceText)

(这允许任何人右键单击show source并查看构建代码时使用的确切Github提交ID).

(This allows anyone to right-click show source and see the exact Github commit ID used when the code was built).

静态类(BuildSupport)公开了一个字符串属性(InstanceText),该属性基本上使用Assembly.GetCallingAssembly来获取程序集,然后获取程序集信息(我们称为BuildStamp).

The static class (BuildSupport) exposes a string property (InstanceText) that basically uses Assembly.GetCallingAssembly to get the assembly and then get the assembly info (which we refer to as BuildStamp).

最初,每个项目都有自己的文件副本(虽然不好,但这还处于初期),然后我们将类移至实用程序集(在Nuget服务器上)并以这种方式进行访问-失败.

Initially each project had its own copy of the file (which was not good but this was early days) then we moved the class into a utility assembly (on our Nuget server) and accessed it that way - this failed.

它显然失败了,因为在这种情况下,调用程序集是实用程序程序集,因此我们最终获得了该程序集的buildstamp,而不是项目程序集.

It failed obviously because the calling assembly in this case IS the utility asssembly, so we end up getting the buildstamp for that assembly rather than the project assembly.

我们现在已经修复"了通过将BuildSupport类放置在仓库的根目录,然后让每个项目本身都引用源文件(作为链接)来实现此目的.

We've now "fixed" this by situating the BuildSupport class at the root of our repo and then have every project itself refer to the source file (as a link).

现在,当我们构建应用程序时,它可以工作-在运行时,BuildSupport类将被编译(当MVC页面被编译时),并成为项目程序集的一部分.

Now when we build our app it works - at runtime the BuildSupport class gets compiled (when the MVC page gets compiled) and becomes part of the project assembly.

我的问题是否有更好的方法-是否有办法将其打包到不同的支持程序集中,还是必须使用所有项目(在其他解决方案中)都称为链接的公共源文件?

My question is there a better way - is there a way to package this into a distinct support assembly or must we use a common source file that all projects (in other solutions too) refer to as a link?

(简而言之,我们通过确保每个程序集包含BuildSupport类来使它公开自己的AssemblyInformationalAttribute的能力-也就是说,我们构建和发布的每个程序集都包含此静态类的副本作为其自身的一部分).

(In short we give each assembly the ability to expose its own AssemblyInformationalAttribute by ensuring it contains the BuildSupport class - that is to say every assembly we build and publish contains a copy of this static class as part of itself).

推荐答案

Hi Korporal Ker nel,

>>我的问题是否有更好的方法-是否有办法将其打包到不同的支持程序集中,或者必须使用所有项目(在其他解决方案中)都称为的公共源文件链接?

根据您的描述,您似乎可以使用静态类创建一个库类项目,而其他项目则引用该库类项目.有关更多信息,请参阅:

According to your description, it seems that you could create a library class project with the static class, and other projects reference the library class project. for more information, please refer to:

https://msdn.microsoft.com/en-us/library/cc668164.aspx

最诚挚的问候,

吴可乐


这篇关于在运行时获取程序集属性的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆