IL assembly code,
.assembly extern mscorlib {}
.assembly MathLib
{
.ver 1 : 0 : 1 : 0
}
.module MathLib.dll
.namespace MyMath
{
.class public ansi auto MathClass extends [mscorlib]System.Object
{
.method public int32 GetSquare(int32) cil managed
{
.maxstack 3
ldarg.0 // 加载对象的 this 指针到堆栈上
ldarg.1 // 实例方法的实际的参数索引总是从 1 开始
ldarg.1
mul
ret
}
}
}
Build as a dll as follows; the dll contains a method that calculates the square of an integer and returns the value;
Then make a test program;
using System;
using MyMath;
class Program
{
static void Main(string[] args)
{
int a=9;
int i = MathClass.GetSquare(a);
Console.WriteLine(i);
Console.ReadKey();
}
}
Build this test program from the command line;
To reference an external dll when building from the command line, you need to use /r to specify the dll file name, see,
C# csc builds dll and specifies dll when csc is built_bcbobo21cn's blog-CSDN blog
Then the following error occurs;
I still don't know how to do it;
First learn about IL assembly instructions;
ldarg.n
Load the nth parameter into the stack; in a non-static function, the 0th parameter is an implicit parameter representing this;
The mul instruction is multiplication;