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.
Share
this.GetType()gets the polymorphic type of the current instance, which may actually be a subclass of the class you’re callingthis.GetType()from, and that subclass may be located in a different assembly.Consider the following:
AssemblyA.dll:
AssemblyB.dll:
Now if you run the following code:
The result of the two ways of determining the assembly will not be the same;
this.GetType().Assemblywill return AssemblyB (because the actual type ofthisisBar), whereasAssembly.GetExecutingAssembly()returns AssemblyA, because that’s the assembly containing theFoo.PrintAssembly()method.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.