Inline Il)
Inline Il can define the function body directly in intermediate language (IL). The intermediate language is F # compiled language, it mainly adds certain low-level operators and functions for the language, such as addition and box and not. We seldom use this function, because the F # library fsharp. Core. dll has published all the functions that may be required to generate intermediate languages. However, some extremely rare cases cannot be completed in F #, but can only be implemented in intermediate languages. inline intermediate languages are a good choice.
It is easy to use the inline intermediate language. You only need to place the intermediate language instructions in the middle of the brackets and the # sign ,(##). The intermediate language directive is placed in the middle of a serial number and uses a standard mark that can be compiled by ilasm.exe. It must be in the correct intermediate language format; otherwise, an error occurs during compilation. Then, the parameter is passed to the intermediate language directive, they are pushed into the language evaluation stack (IL evaluation stack); Standard colons must also be used to tell the compiler what type is returned, which should be placed in the middle of the brackets; you also need to explicitly specify the parameter types because the compiler cannot specify their types.
Now let's take a look at the example of using inline intermediate language. For some reason, we need to use the addition and subtraction operators defined in the F # basic library fslib. DLL to replace them with our own defined functions. We have defined two functions, add and sub. The function body is defined in the intermediate language:
// Declare add functionusing the Il add instruction
Let add (X: INT) (Y: INT) = (# "add" x y: int #)
// Declare sub functionusing the Il sub instruction
Let sub (X: INT) (Y: INT) = (# "sub" x y: int #)
// Test these functions
Let X = Add 1 1
Let y = sub 4 2
// Print the results
Printfn "X: % I y: % I" X Y
The running result of the example is as follows:
X: 2 Y: 2
The programmer should be careful when using this method, because writing a meaningless program is not important and the compiler cannot give a warning. Considering the following program, we modified the previous example and replaced the "add" command with the "RET" command, which indicates that the returned value is meaningless in this context. This example does not contain errors and warnings during compilation, but it may cause errors during runtime.
// Create a faulty add function
Let add (X: INT) (Y: INT) = (# "RET" x y: int #)
// Attempt to use fault Function
Let X = Add 1 1
The running result of the example is as follows:
Unhandled exception: system. invalidprogramexception: Common Language Runtime
Detected an invalid program.
At error. Add (int32 X, int32 y)
Note:
With. Net SDK, a tool called peverify.exe can help detect this error. For more information about this tool, see http://msdn2.microsoft.com/en-us/library/62bwd2yd (vs.80). aspx.