This post originated from an RSS feed registered with .NET Buzz
by Adrian Florea.
Original Post: Because the parent does not exist
Feed Title: Web Log di Adrian Florea
Feed URL: /error.aspx?aspxerrorpath=/adrian/Rss.aspx
Feed Description: "You know you've achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away." Antoine de Saint-Exupery
Pensavate che non si possa scrivere una classe in C# che non derivi da nessun'altra classe, vero? :-) Provate a compilare questo codice:
namespace System { class Object {} }
Passando l'assembly all'ildasm, notiamo due cose "strane":
.namespace System { .class private auto ansi beforefieldinit Object { .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { .maxstack 0 br.s here here: ret } } }
è sparita la clausa extends [mscorlib]System.Object (dobbiamo prendere sul serio il commento "note that class flags, 'extends' and 'implements' clauses are provided here for information only"? - vedi anche questo altro mio post);
il costruttore non chiama il costruttore della classe base - magari perché non esiste più una classe base :-)
Se verifichiamo l'assembly con peverify, otteniamo:
[IL]: Error: [token 0x02000002] Type load failed.
Vediamo se riusciamo ad ottenere qualche informazione in più provando a consumare la classe, cioè ad eseguire lo snippet modificato come di seguito:
namespace System { class Object{} }
class Foo { static void Main() { System.Console.WriteLine(typeof(System.Object)); } }
Compilando, otteniamo il warning CS1595 (level 1):
warning CS1595: 'object' is defined in multiple places; using definition from '<mia path>\foo.cs'
ed eseguendo, l'errore:
Unhandled Exception: System.TypeLoadException: Could not load type System.Object from assembly Foo, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null because the parent does not exist.
"because the parent does not exist" - ho provato a fare un ri-assembing del codice IL generato dopo il dis-assembling però dopo il dis-assembling del ri-assembling ho ottenuto lo stesso codice IL. Pensavo di vedere la clausa extends [mscorlib]System.Object e invece ho scoperto che questa clausa è implicita per tutte le classi che non estendono un altra classe, tranne il caso in cui la classe si chiami proprio System.Object.
In Visual Basic .NET, non si riesce a compilare:
Namespace System Class Object End Class End Namespace
perché otteniamo l'errore BC2010:
Command line error BC2010 : compilation failed : '0xC0000005'
Non ho capito perché "command line".
(Aggiornamento 20/04/05): e questo 0xC0000005 ha odore di "access violation"