Assembly.GetExecutingAssembly()的性能 [英] Assembly.GetExecutingAssembly() performance

查看:1354
本文介绍了Assembly.GetExecutingAssembly()的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待到建立一个自动化的方式来检查我们的系统(S)的运行版本。每当我们发布code测试版本,我们用

I'm looking into creating a automated way to check the running version of our system(s). Whenever we release code to test we version is using

[assembly: AssemblyVersion("1.21.0.13329")]
[assembly: AssemblyFileVersion("1.21.0.13329")]

我现在想要扩展我们的ASP.Net的 Web表单网络服务,以显示该版本号。因此,Web表单应用程序将会把它放在主页上和Web服务可能不得不返回版本号的Web方法。

I now want to extend our ASP.Net Web forms and Web Services to display this version number. So the Web forms app will put it somewhere on the home page and the web service will probably have a web method that returns the version number.

我想使用

Version version = Assembly.GetExecutingAssembly().GetName().Version;

做到这一点。但我担心,因为这是使用反射可能性能开销添加到我的code。它可以获取调用几次,特别是在主页上,我不希望这种响应时间慢了下来。

to do this. But I'm concerned that because this is using reflection it could add a performance overhead to my code. It could be getting called several times, especially on the home page and I don't want this to slow response times down.

我有另一种解决办法,所以我不想选择,我只是想知道是否有人知道,如果这样的性能开销将是相对显著?

I have another solution so I don't want alternatives, I just want to know if anyone knows if the performance overhead of this is going to be relatively significant?

推荐答案

的性能开销,无论是显著真正的问题是取决于它有多少访问。在一个单页的打击,可能不是 - 在code执行时间在一些毫秒被测量。如果服务器负载下有很多的点击,很可能。

The question of the performance overhead and whether it is significant is really dependent on how much it is accessed. On a single page hit, probably not - the code execution time being measured in some ms. If the server is under load with lots of hits, quite possibly.

但你不能只让打一次呢(即仍使用反射,但不是每次):

But can't you just make the hit once anyway (i.e. still use reflection, but not every time):

我猜想这不会在Web应用程序实例的生命改变 - 那么为什么不一次捕获此信息并储存起来。

I'm assuming this won't change over the life of a web application instance - so why not capture this information once and store it?

也许创建一个类来捕捉这一点,如:

Perhaps create a class to capture this, such as:

public class AppVersion
{
    private static readonly Version _applicationVersion = Assembly.GetExecutingAssembly().GetName().Version;

    public static Version ApplicationVersion
    {
        get { return _applicationVersion; }
    }
}

您现在可以通过访问该在code:

You can now access this in code via:

var version = AppVersion.ApplicationVersion

这篇关于Assembly.GetExecutingAssembly()的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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