У вас небольшая проблема: ваш .ctor
должен вызвать ctor базового объекта (ldarg.0
, call instance void [System.Private.CoreLib]System.Object::.ctor()
, ref
). И для программы, и для функций
Основная проблема, однако, заключается в том, что вы пытаетесь вызвать add5mul
на ... ничего. В стеке нет объекта Functions
для вызова.
// Push Functions instance onto stack
// Stack: [functions]
newobj instance void Functions::.ctor()
// Push 3 onto stack
// Stack: [3, functions]
ldc.i4 3
// Pop 3 and functions off the stack
// Stack: []
call instance void Functions::Add(int32)
// Push 5 and 3 onto stack
// Stack: [3, 5]
ldc.i4 5
ldc.i4 3
// Pop 5, 3, and... nothing. We're missing the Functions instance to call it on.
call instance int32 Functions::add5mul(int32,int32)
Это можно исправить, продублировав экземпляр Functions перед его первым использованием:
newobj instance void Functions::.ctor()
dup <-- Here
ldc.i4 3
call instance void Functions::Add(int32)
ldc.i4 5
ldc.i4 3
call instance int32 Functions::add5mul(int32,int32)
call void [mscorlib]System.Console::WriteLine(int32)
ret
Вы также можете сохранить этот экземпляр функции в локальном слоте:
.method static void Main(string[] args)
cil managed
{
.locals init (
[0] class Functions
)
.entrypoint
newobj instance void Functions::.ctor()
stloc.0
ldloc.0
ldc.i4 3
call instance void Functions::Add(int32)
ldloc.0
ldc.i4 5
ldc.i4 3
call instance int32 Functions::add5mul(int32,int32)
call void [mscorlib]System.Console::WriteLine(int32)
ret
}
SharpLab.io - отличный ресурс для изучения IL. Вот ваш код, переведенный в C# и декомпилированный в IL .