Is system. object the root of all types in. Net?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.