Chapter 1 simple example 2.0 simple example (III) of expert. Net 1.2 il Explorer)

Source: Internet
Author: User
Tags mscorlib

Returned directory

 

Global items

Below isOddorevenApplicationProgramGlobal items:

These are the global items of the oddoreven Application:
{

}
// End of namespace
. Field   Public Static valuetype chararray8 format at formatdata

. Field public static valuetype chararray8 format at formatdataDeclared A typeValuetype chararray8Name:Format. As you may rememberOdd. Or. Even: CheckThe reference pointing to this field is used in the method.

For example, this field is different fromOdd. Or. Even: ValBecause it is declared outside the scope of the class, it does not belong to any class. It is called a global item (Global item). Global items of this module, including their declarations. As you know, a module is a managed executable file (EXEOrDLL); One or more modules form an assembly. An assembly is the main building block for hosting applications. Each assembly has a main module, it carries the Assembly identification information in the metadata.

In fact, a trick is associated with the concept of global items that do not belong to any class. The metadata of each module includes a special<Module>OfTpyedef, Represents -- what can I guess? Yes, you can guess it.

TypedefAlways exist in metadata, and it is always maintained inTypedefThe first position in the table. However,<Module>Not a properTypedefBecause it is compared with "normal"Typedef(Class, value type, etc.), its features are limited. It sounds like a real life-the more you are at the top of the list, the more limited your choice.

<Module>Cannot bePublicThat is, it is invisible outside its assembly.<Module>Only static members are allowed. This means that all global fields and methods must be static. In addition,<Module>Fields and attributes cannot be static. (Refer to Section15Chapter to obtain details .) The reason for the restriction is obvious: Each module in a given Assembly always includes an instance, and the concept of Instantiation becomes meaningless.

accessibility of global fields and methods, different from the accessibility of member fields and methods of a "normal" class. Even public global items cannot be accessed outside the assembly. there is no extension -- it does not have a base class -- and no class can be derived from . However, all declared classes in the module have full access to the global items in the module, including private global items.

the last feature is similar to the class embedding and is different from the class inheritance. (The derived class does not have access to the private items of the base class .) An embedded class is a class declared in the range of another class. Other classes are usually called outsourcing class ( enclosing class ) or shell ( encloser ). An embedded class is not a member class or an internal class. From this perspective, it cannot implicitly access this outsourcing instance reference of the class ( This ). An embedded class can only be connected to its peripheral class in three cases: declare the embedded class in outsourcing class lexical range; the visibility of embedded classes is achieved through outsourcing class visibility filtering (that is, if outsourcing the class is private, and the embedded class is invisible to the external assembly, regardless of its own visibility); embedded classes can access outsourcing all members of the class.

because all classes declared in the module are defined within the lexical range of the module, therefore, it is logical to understand the relationship between modules and declared classes as the relationship between outsourcing classes and their Nested classes.

result: the Global item accessable modifier Public , Assembly and famorassem is equivalent to Assembly ; private , family and famandassem equivalent to private ; and privatescope or privatescope . The metadata verification rules clearly stipulate that only three accessible modifiers are allowed for global fields and global Methods: Public ( Assembly ), private and privatescope . However, the loader has more loose requirements for accessible modifiers for global items: it allows you to set any accessable modifiers, and as described above (for example, Assembly , private or privatescope .

Ing field

Below isOddorevenApplication ing fields:

. Field   Public Static valuetype chararray8 format at formatdata

FormatThe field Declaration includes a new one:At formatdataWords. This clause indicatesFormatThe field is located in the module's data segment, and its position isFormatdataThis data tag is identified. (The following sections will discuss data declaration and tagging)

The compiler extensively uses the technology of ing fields to data for field initialization. This technology has no restrictions. First, the mapped field must be static. This is logical. After all, the ing itself is static because it occurs during the compilation period. Even if you decide to map an instance field, all different instances of this field will be naturally mapped to the same memory, which means you still need to use static fields in the end. Because the loader encounters a field mapped to the instance, it will decide to support "instantiation" and completely ignore field ing, the mapped instance field is displayed as all other instance fields.

Second, the ing field is located in the data segment.CLRThe garbage collection sub-system is inaccessible, and it will automatically destroy useless objects. For this reason, the ing field is not the type of garbage collection (such as similar or array ). Value types can also be used as the type of ing fields. As long as there are no member types in these value types, they are actively collected for garbage collection. If this rule is violated, the loader throws a Type Loading exception and stops loading this module.

Second, map a field to a pre-allocated memory location, so that this field supports read and write extensively. This is very good From the security perspective, as long as this field does not have an internal structure-a part of this structure is not intended for public access. This is why the ing field type cannot be any value type with non-public field members. The loader strictly executes this rule and keeps checking for non-public fields. For example, if the field to be mapped is of the Value TypeA, The loader will check whether all its fields are public. If there is a value type in these fieldsB, The loader will checkBAre all public fields. If there are two value type fields in these fieldsCAndD-- Well, you should be able to understand what I mean. If the loader finds a non-public field on the ing field type at any level, it will throw a Type Loading exception and terminate the loading.

Data Declaration

Below isOddorevenApplication Data declaration:

. Field   Public Static valuetype chararray8 format at formatdata
. Data Formatdata = bytearray ( 25   64   00   00   00   00   00   00 )

. Field public static valuetype chararray8 format at formatdataMarkFormatdata. This section has8Length of BITs, the first two of which areASCIICharacters in the form of codes%(0x25) AndD(0x64), The remaining6BITs are0.

This section is usedBytearrayTo describe, this isIlasmThe common way to describe data. The number in parentheses represents the byte16Hexadecimal value0xPrefix. Byte values should be separated by spaces. We recommend that you always use the form of two digits, even if one digit is sufficient (as shown in the example ).0In this case ).

Obviously, you can represent any data in text asBytearray. For exampleLdstr "enter a number"Use the referenced string, but useBytearrayIndicates:Ldstrbytearray (6f 00 64 00 64 00 21 00 00 00)

The numbers in parentheses indicateUnicodeCharacter:O,D,D,!, And0As a break. When you useIldasmYou will findBytearrayIs everywhere.BytearrayIs a common, type-independent (Type-neutral), WhenIldasmThe data type cannot be recognized as a primitive type (for exampleInt32), It is represented by a byte array.

On the other hand, you can defineFormatdataThe data is as follows:

. Data formatdata = int64 (0x0000000000006425)

This results in the same data segment size and content. When you specify a Data Segment declared (for exampleInt64), There is no relevant record. This type is put into metadata or somewhere else.IlasmThe compiler uses a specific type to identify the size of data segments and the appearance of the bytes in the segments.

 

Value Type as a placeholder

The value types used as placeholders are as follows:

. Field   Public Static valuetype chararray8 format at formatdata
. Data Formatdata = bytearray ( 25   64   00   00   00   00   00   00 )
. Class   Public Explicit chararray8
Extends [Mscorlib] system. valuetype {. Size 8 }

. Class public explicit chararray8 extends [mscorlib] system. valuetype {. Size 8}Declared a value type without members, but there is a display of the specified size:8Bytes. Declaring such a value type is to declare"Only one memory. In this case, you do not need to declare any Member of the value type, because you are not interested in the internal structure of this memory; you just want to simply use it as a global field that specifies the field sizeFormat. In a sense, the value type is just a placeholder.

You can use8Does the byte array not need to declare another value type? If you do not plan to map fields to data, you can. Arrays are actively collected for garbage collection, so they are not allowed as the type of ing fields.

Use the value type as a placeholderC/C ++Compiler is very popular, because a large numberANSIString constants store and record their addresses.Visual C #AndVisual Basic. netCompiler, mainly ProcessingUnicodeString, not very concerned about this technology, because they are directly usedCLRString constant -- this will useUnicodeFormat is stored in metadata.

 

Call unmanagedCode

Below isOddorevenHow the application declares unmanaged code will be managedCheckCall:

. Method   Public Static pinvokeimpl ( " Msvcrt. dll " Cdecl)
Vararg Int32 Sscanf ( String , Int8 *) Cel   Managed {}

The above declares an unmanaged method called by the managed code.Pinvokeimpl ("msvcrt. dll" cdecl)This is an unmanaged methodP/inkoke(Platform Invocation. This feature also points out that this method resides in an unmanagedMsbcrt. dllAnd hasCdecl. This call Convention means that the method for processing parameters in an unmanaged method isANSI CFunctions are the same.

This method has twoStringType andInt8 *Type (equivalentC/C ++OfChar *) And returnInt32. AsVarargMethod,SscanfThere can be any number of optional parameters of any type, but as you knowVarargOptional parameters orSentinelAre not specified.

P/inkokeA mechanism is as follows:CLRIt provides convenience for calling unmanaged code from managed code. Behind this scenario, the runtime constructs the so-calledStubOrThunkThis will allow recording of the address of an unmanaged function, as well as the Convention for conversion between a hosted parameter type and an appropriate unmanaged type. This Convention is called a parameter (Param messages sent to eter login aling).

here, we declare not only an exact called unmanaged method, instead, it is a placeholder program generated by the runtime ( stub ), as you can see in the managed code, the implementation of the pencil managed mark is explained. Specify the method signature as int32 (string, int8 *) , you have specified the "managed part" ( managed side ) of the mail parameters ). The unmanaged part of the mail parameters is defined by the exact signature of the called unmanaged method.

C language, sscanf . Therefore, the first parameter is the string type mails are sent to unmanaged char * types. Recall when we declare odd. or. when the even class is specified, I specify the ANSI flag, this means that the hosted string is ansi c string by default: char * . The sscanf is called by odd. or. even is generated by using the member method. You do not need to provide special information about the encapsulated string.

sscanf the second parameter declaration is int8 * , which is directly equivalent to char * . Results are rarely sent. ( ilasm also has char type, however, it indicates a Unicode character instead of ANSI , equivalent to C the language" unsigned short ", so you cannot use this type here .)

original (unmanaged) sscanf is considered as a pointer, pointing to the item (variable) You want to fill when parsing a buffer string ). The number and basic types of these pointers are defined based on the second parameter of the formatted standard string ( sscanf . In this case, a formatted standard string " % d " is given ", sscanf A separate optionality int * type parameter. When I call sscanf -- this managed thunk I provide optional parameters of the int32 * type, this may need to be grouped into a local integer pointer, only when the platform you are processing is not a 32 bit intel platform time (for example, amd or a 32 bit intel Platform ).

Bag description:ThunkTechnology is generally considered to be a technology that directly constructs executable code in a program (normally, this is a compiler task ).

P/inkokeThe mechanism is very useful because it allows you to access a large number of local libraries and platforms.API. But do not overestimate the presence of thisP/inkoke. Different platforms lead to theirAPIDifferent, so excessive useAPILimits the portability of applications. It is best to stick to it. NET FrameworkThe Class Library also provides some comfort-Think about what you can think about at the bottom of the class library so far.

Now that I have completedSource codeFor more information, seeApressWebsite:Http://www.apress.com /., Get the source fileSimple. Il, Copy it to your working directory, and use Console CommandsIlasm simpleCompile (assuming you have installed. NET FrameworkAndSDK), Try to runSimple.exeThis compilation result.

 

 

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.