Is system. object the root of all types in. Net?
Written by Allen Lee
Isn't that nonsense? When you access. net, you will be reminded that all types are directly or indirectly inherited from system. object, even though msdn records similar words. Is that true? If you enter ilasm/? On the command line interface /?, You will find such a parameter:
/Noautoinherit disable inheriting from system. object by default
Now, I try to use this parameter to create a managed type without a base class. First, open notepad and write down the following:Code:
// Adam. CS
Public Class Adam
{
Public VoidHello ()
{
System. Console. writeline ("I have no parents!");
}
}
Use CSC/T: Library Adam. CS to compile Adam. cs into Adam. dll. Next, write the following code in Notepad:
// Program. CS
Class Program
{< br> static void main ()
{< br> Adam = New Adam ();
Adam. hello ();
}
}
Use CSC/R: Adam. DL program. CS to compile program. cs into program.exe. Run thisProgramTo get the following output:
I have no parents!
Of course, Adam does not really have a parent at this time. Now, open Adam. dll with ildasm, select File> dump, click OK, and save the Il code as Adam. il. Next, open Adam. Il in notepad, find the Adam definition, comment out extends [mscorlib] system. object after it, save and exit. Then, enter ilasm/dll/noautoinherit Adam. Il in the command line to obtain the new Adam. dll. Now, run program.exe again. Although the output is the same as before, when you use ildasm to view the Adam class in Adam. dll, you will find that it has become "Real Adam! Even reflector says it has no base types ~~~
However, I can't help asking myself, what is the use of such a class? If it is useless, why does Microsoft introduce a parameter like/noautoinherit in 2.0?