Reputation: 2516
While debugging an issue I was digging in to mscorlib and specifically at GetName()
in System.Reflection.Assembly
.
Looking at the code for the method in dotPeek and also on reference source the code for GetName()
seems to have nothing in it and throws a NotImplementedException
:
#if FEATURE_CORECLR
[System.Security.SecurityCritical] // auto-generated
#endif
public virtual AssemblyName GetName()
{
return GetName(false);
}
#if FEATURE_CORECLR
[System.Security.SecurityCritical] // auto-generated
#endif
public virtual AssemblyName GetName(bool copiedName)
{
throw new NotImplementedException();
}
Can anyone explain why this appears to throw an exception, but using a line of code such as typeof(object).GetTypeInfo().Assembly.GetName().Version.ToString()
returns the correct version of the DLL?
Upvotes: 0
Views: 274
Reputation: 391406
That's because Assembly
is a base class, the actual object you get back at runtime (in this particular setting) is a RuntimeAssembly
which does implement that method.
Depending on how you obtain the Assembly
object, there are different actual types at play, be it for actual runtime code or reflection-only spelunking or whatnot.
But Assembly
is a base class.
I can say this unequivocally because the Assembly
class is abstract, as evident from the reference source:
public abstract class Assembly : _Assembly, IEvidenceFactory, ICustomAttributeProvider, ISerializable
And it's also documented:
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Assembly : System.Reflection.ICustomAttributeProvider, System.Runtime.InteropServices._Assembly, System.Runtime.Serialization.ISerializable, System.Security.IEvidenceFactory
Therefore, any instance of an object that fits an Assembly
variable has to be a derived class.
Upvotes: 3