简体   繁体   English

这个.GetType()。Assembly.GetName()。版本和Assembly.GetExecutingAssembly()。GetName()。版本有什么区别?

[英]What's the difference between this.GetType().Assembly.GetName().Version and Assembly.GetExecutingAssembly().GetName().Version?

As the title suggests, how do these two differentiate with each other? 正如标题所示,这两者如何相互区别? Are we safe to say they both the same? 我们可以安全地说他们俩一样吗? When is the best case where we choose one over the other? 什么时候我们选择其中一个是最好的情况? I just happened to come across it and I wasn't really sure. 我碰巧碰到了它,我不太确定。 I hope someone can clear my doubts. 我希望有人能清除我的怀疑。 Thanks in advance. 提前致谢。

this.GetType() gets the polymorphic type of the current instance, which may actually be a subclass of the class you're calling this.GetType() from, and that subclass may be located in a different assembly. this.GetType()获取当前实例的多态类型,它实际上可能是您正在调用this.GetType()类的子类,并且该子类可能位于不同的程序集中。

Consider the following: 考虑以下:

AssemblyA.dll: AssemblyA.dll:

public class Foo
{
    public void PrintAssembly()
    {
        Console.WriteLine(this.GetType().Assembly.GetName());
        Console.WriteLine(Assembly.GetExecutingAssembly().GetName());
    }
}

AssemblyB.dll: AssemblyB.dll:

public class Bar : Foo
{
}

Now if you run the following code: 现在,如果您运行以下代码:

Bar b = new Bar();
b.PrintAssembly();

The result of the two ways of determining the assembly will not be the same; 两种确定装配方式的结果将一样; this.GetType().Assembly will return AssemblyB (because the actual type of this is Bar ), whereas Assembly.GetExecutingAssembly() returns AssemblyA, because that's the assembly containing the Foo.PrintAssembly() method. this.GetType().Assembly将返回AssemblyB(因为实际的类型thisBar ),而Assembly.GetExecutingAssembly()返回AssemblyA,因为这是包含组装Foo.PrintAssembly()方法。

The only time you can be certain that they refer to the same assembly is if the type containing the call to this.GetType() is sealed. 只有当包含对this.GetType()的调用的类型被密封时,才能确定它们引用相同的程序集。

One tells you the version of the Assembly the Type belongs to. 一个告诉你类型属于的程序集的版本。 The other tells you the version of the assembly that is currently executing. 另一个告诉您当前正在执行的程序集的版本。 But you knew that already. 但是你已经知道了。

I believe you can safely assume that the executing assembly is always going to be the same as the assembly that 'this' is part of. 我相信你可以放心地假设执行程序集总是与'this'所属的程序集相同。 At least I can't think of why it wouldn't be. 至少我想不出为什么不会这样。

Whether you choose one or the other, for clarity sake, would be dependent upon whether you're looking for the assembly of the type, or the assembly that's executing. 为清楚起见,无论您选择其中一个还是另一个,都取决于您是在寻找类型的装配还是正在执行的装配。 Let's say your dad and your boss are the same person... do you refer to him as your boss at the dinner table? 假设你的父亲和你的老板是同一个人...你在餐桌上称他为你的老板吗? Or do you introduce him to your girlfriend as your boss? 或者你把他介绍给你的女朋友作为你的老板? Use the one that's going to make sense to the next person who reads your code. 使用对下一个读取代码的人有意义的那个。

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

相关问题 什么情况下 Assembly.GetName().Name 可以是 null? - Under what conditions can Assembly.GetName().Name be null? Assembly.GetExecutingAssembly()和typeof(程序)之间的区别.Assembly - Difference between Assembly.GetExecutingAssembly() and typeof(program).Assembly 获取System.Reflection.Assembly.GetExecutingAssembly()。GetName()。Version.ToString(); 来自XAML - Get System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); from XAML 无法从我的Silverlight应用程序调用Assembly.GetName() - Unable to call Assembly.GetName() from my Silverlight application Windows phone 8.1 Assembly.GetExecutingAssembly不可用 - Windows phone 8.1 Assembly.GetExecutingAssembly not available 为什么 Assembly.GetExecutingAssembly() 会返回 null? - Why would Assembly.GetExecutingAssembly() return null? 在纯XAML中获取程序集版本GetExecutingAssembly - Get Assembly Version in Pure XAML GetExecutingAssembly 使用Assembly.GetExecutingAssembly()从其他类库获取程序集 - Use Assembly.GetExecutingAssembly() to get assembly from other class library C# - 如何模拟this.GetType()。程序集 - C# - How to mock this.GetType().Assembly Assembly.GetExecutingAssembly()。CreateInstance应该在这里抛出异常吗? - should Assembly.GetExecutingAssembly().CreateInstance throw an exception here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM