View C # Through IL (another article)-guard against constant traps

Source: Internet
Author: User
Tags mscorlib
View C # Through IL)
Guard against constant traps

Address: http://www.cnblogs.com/AndersLiu/archive/2008/11/23/csharp-via-il-constant-a.html

Original: Anders Liu

Abstract: The meaning of a constant is "the amount that will never change". However, if you are a class library developer, you can use a constant as the quantity that can be changed by me, but cannot be changed by you, that could be a big mistake.

The following is a class in a class library written by Lao Liu:

Code 1-Liu's "class library"

Namespace andsliu. CSharpViaIL. constant_Library <br/>{< br/> public class Library <br/>{< br/> public const int Version = 1; </p> <p> public string GetVersion () <br/>{< br/> return string. format ("You are currently using version {0 }. ", Version); <br/>}< br/>}

The constant Version indicates the current Version of the class library, and the GetVersion method provides a string describing the current Version. Use the following command line to compile it into a DLL:

Csc/t: library. cs

Next, an unlucky guy bought this class library from Liu and wrote his own program:

Code 2-Liu's "consumer"

Namespace andsliu. CSharpViaIL. constant_Program <br/>{< br/> using System; <br/> using Andersen sliu. CSharpViaIL. constant_Library; </p> <p> class Program <br/>{< br/> static void Main () <br/>{< br/> Console. writeLine ("Library Version: {0}", Library. version); </p> <p> Library lib = new Library (); <br/> string verstr = lib. getVersion (); <br/> Console. writeLine (verstr); <br/>}< br/>}

This guy compiled his program using the following command line:

Csc/r: Library. dll Program. cs

Run the program, everything is normal, the screen shows the expected results:

Library Version: 1
You are currently using version 1.

After two days, Liu upgraded the code. Which parts have been upgraded? Change the Version value to 2, and then re-compile the Library. So the code will not be pasted. This guy had bought a genuine copy from Liu because he was very good at it. He promised to upgrade it 50 times for free, so he got the new Library. dll.

When this guy runs his program again (note that he hasn't re-compiled his code), the problem arises:

Library Version: 1
You are currently using version 2.

We can see that the version obtained through constant access is still 1, and the version string obtained through the class library method is 2.

What's going on? Let's take a look at the definition of the Version constant by offering ILDasm:

Code 3-Definition of constants in IL

. Field public static literal int32 Version = int32 (0x00000002)

The keyword literal in the Version definition indicates that this is a literal constant value. "Literal" means that the value will be directly compiled into the IL code without retaining the reference to this constant.

Therefore, when we see the IL code of the Main method in Program. cs, we are not so surprised:

Code 4-The IL code that references the Version constant

IL_0001: ldstr "Library Version: {0}" <br/> IL_0006: ldc. i4.1 <br/> IL_0007: box [mscorlib] System. int32 <br/> IL_000c: call void [mscorlib] System. console: WriteLine (string, <br/> object) <br/>

We can see that there is no shadow of the constant Version. Only the IL_0006: ldc. i4.1 line loads the value 1 directly -- the value of the Version constant in the class library when compiling the Program.

Some friends may want to say that re-compiling the Program will not solve the problem? But the problem is that an important principle of. NET is that it is easy to deploy and eliminate DLL traps. It is not mandatory for customers to re-compile their own programs after class library upgrade.

Therefore, for the class library designer, be very careful when exposing a public constant and use the constant only when you are sure that a value will never change (including a series of future upgrades. Otherwise, use the read-only (only get accessors) attribute or the read-only (readonly) field to return the value.

Returned Directory: view C # Through IL #

Related Article

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.