this.GetType() gets the polymorphic type of the current instance, which can actually be a subclass of the class that you call this.GetType() , and this subclass can be located in another assembly.
Consider the following:
AssemblyA.dll:
public class Foo { public void PrintAssembly() { Console.WriteLine(this.GetType().Assembly.GetName()); Console.WriteLine(Assembly.GetExecutingAssembly().GetName()); } }
AssemblyB.dll:
public class Bar : Foo { }
Now, if you run the following code:
Bar b = new Bar(); b.PrintAssembly();
The result of the two methods for determining the assembly will not be the same; this.GetType().Assembly will return AssemblyB (since the actual type of this is Bar ), while Assembly.GetExecutingAssembly() returns AssemblyA because the assembly contains the Foo.PrintAssembly() method.
The only time you can be sure that they reference the same assembly is that the type containing the call to this.GetType() is sealed.
source share