您可能不想使用以下任何一种:
dynamic
- 表达式树
DynamicMethod
因为与整数算术相比,它们都非常慢(它们使用委托)。
最好的办法是在 MSIL 中使用 、 等算术函数进行程序集Add
,Subtract
然后从 C# 代码中调用它们。
事实上,事实证明我已经为你完成了你的工作:
// Compile with:
// C:\Windows\Microsoft.NET\Framework\v2.0.50727\ilasm.exe Arithmetic.il /dll
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 2:0:0:0
}
.assembly Arithmetic
{
}
.module Arithmetic.dll
.subsystem 0x0003
.class public abstract auto ansi sealed beforefieldinit Helper.Arithmetic
extends [mscorlib]System.Object
{
.method public hidebysig static !!T Or<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
or
ret
}
.method public hidebysig static !!T And<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
and
ret
}
.method public hidebysig static !!T Xor<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
xor
ret
}
.method public hidebysig static !!T Not<T>(!!T val) cil managed
{
.maxstack 1
ldarg.0
not
ret
}
.method public hidebysig static !!T Add<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
add
ret
}
.method public hidebysig static !!T AddOverflow<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
add.ovf
ret
}
.method public hidebysig static !!T AddOverflowUnsigned<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
add.ovf.un
ret
}
.method public hidebysig static !!T Subtract<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
sub
ret
}
.method public hidebysig static !!T SubtractOverflow<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
sub.ovf
ret
}
.method public hidebysig static !!T SubtractOverflowUnsigned<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
sub.ovf.un
ret
}
.method public hidebysig static !!T Multiply<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
mul
ret
}
.method public hidebysig static !!T MultiplyOverflow<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
mul.ovf
ret
}
.method public hidebysig static !!T MultiplyOverflowUnsigned<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
mul.ovf.un
ret
}
.method public hidebysig static !!T Divide<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
div
ret
}
.method public hidebysig static !!T DivideUnsigned<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
div.un
ret
}
.method public hidebysig static !!T Remainder<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
rem
ret
}
.method public hidebysig static !!T RemainderUnsigned<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
rem.un
ret
}
.method public hidebysig static bool Equals<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
ceq
ret
}
.method public hidebysig static bool IsLessThan<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
clt
ret
}
.method public hidebysig static bool IsLessThanUnsigned<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
clt.un
ret
}
.method public hidebysig static bool IsLessThanOrEqualTo<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
cgt
ldc.i4.0
ceq
ret
}
.method public hidebysig static bool IsLessThanOrEqualToUnsigned<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
cgt.un
ldc.i4.0
ceq
ret
}
.method public hidebysig static bool IsGreaterThan<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
cgt
ret
}
.method public hidebysig static bool IsGreaterThanUnsigned<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
cgt.un
ret
}
.method public hidebysig static bool IsGreaterThanOrEqualTo<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
clt
ldc.i4.0
ceq
ret
}
.method public hidebysig static bool IsGreaterThanOrEqualToUnsigned<T>(!!T a, !!T b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
clt.un
ldc.i4.0
ceq
ret
}
.method public hidebysig static !!T ShiftLeft<T>(!!T a, int32 b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
shl
ret
}
.method public hidebysig static !!T ShiftLeft<T>(!!T a, uint32 b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
shl
ret
}
.method public hidebysig static !!T ShiftLeft<T>(!!T a, native int b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
shl
ret
}
.method public hidebysig static !!T ShiftLeft<T>(!!T a, native uint b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
shl
ret
}
.method public hidebysig static !!T ShiftRight<T>(!!T a, int32 b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
shr
ret
}
.method public hidebysig static !!T ShiftRight<T>(!!T a, uint32 b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
shr
ret
}
.method public hidebysig static !!T ShiftRight<T>(!!T a, native int b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
shr
ret
}
.method public hidebysig static !!T ShiftRight<T>(!!T a, native uint b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
shr
ret
}
.method public hidebysig static !!T ShiftRightUnsigned<T>(!!T a, int32 b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
shr.un
ret
}
.method public hidebysig static !!T ShiftRightUnsigned<T>(!!T a, uint32 b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
shr.un
ret
}
.method public hidebysig static !!T ShiftRightUnsigned<T>(!!T a, native int b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
shr.un
ret
}
.method public hidebysig static !!T ShiftRightUnsigned<T>(!!T a, native uint b) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
shr.un
ret
}
.method public hidebysig static native uint DivideCeiling(native uint a, native uint b) cil managed
{
.maxstack 4
ldarg.0
ldarg.1
ldc.i4.1
conv.u
sub.ovf.un
add.ovf.un
ldarg.1
div.un
ret
}
.method public hidebysig static uint32 DivideCeiling(uint32 a, uint32 b) cil managed
{
.maxstack 4
ldarg.0
ldarg.1
ldc.i4.1
sub.ovf.un
add.ovf.un
ldarg.1
div.un
ret
}
.method public hidebysig static uint64 DivideCeiling(uint64 a, uint64 b) cil managed
{
.maxstack 4
ldarg.0
ldarg.1
ldc.i8 0x1
sub.ovf.un
add.ovf.un
ldarg.1
div.un
ret
}
.method public hidebysig static native int DivideCeiling(native int a, native int b) cil managed
{
.maxstack 4
ldarg.0
ldarg.1
ldc.i4.1
conv.i
sub.ovf.un
add.ovf.un
ldarg.1
div.un
ret
}
.method public hidebysig static int32 DivideCeiling(int32 a, int32 b) cil managed
{
.maxstack 4
ldarg.0
ldarg.1
ldc.i4.1
sub.ovf.un
add.ovf.un
ldarg.1
div.un
ret
}
.method public hidebysig static int64 DivideCeiling(int64 a, int64 b) cil managed
{
.maxstack 4
ldarg.0
ldarg.1
ldc.i8 0x1
sub.ovf.un
add.ovf.un
ldarg.1
div.un
ret
}
.method public hidebysig static !!T Lerp<T>(!!T min, !!T weight, !!T max) cil managed
{
.maxstack 3
ldarg.0
ldarg.1
ldarg.0
sub
ldarg.2
mul
add
ret
}
.method public hidebysig static !!T LerpOverflow<T>(!!T min, !!T weight, !!T max) cil managed
{
.maxstack 3
ldarg.0
ldarg.1
ldarg.0
sub.ovf
ldarg.2
mul.ovf
add.ovf
ret
}
.method public hidebysig static !!T LerpOverflowUnsigned<T>(!!T min, !!T weight, !!T max) cil managed
{
.maxstack 3
ldarg.0
ldarg.1
ldarg.0
sub.ovf.un
ldarg.2
mul.ovf.un
add.ovf.un
ret
}
.method public hidebysig static bool IsBetween<T>(!!T 'value', !!T min, !!T max) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
clt
ldarg.0
ldarg.2
cgt
or
ldc.i4.0
ceq
ret
}
.method public hidebysig static bool IsBetweenUnsigned<T>(!!T 'value', !!T min, !!T max) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
clt.un
ldarg.0
ldarg.2
cgt.un
or
ldc.i4.0
ceq
ret
}
.method public hidebysig static bool IsStrictlyBetween<T>(!!T 'value', !!T min, !!T max) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
cgt
ldarg.0
ldarg.2
clt
and
ret
}
.method public hidebysig static bool IsStrictlyBetweenUnsigned<T>(!!T 'value', !!T min, !!T max) cil managed
{
.maxstack 2
ldarg.0
ldarg.1
cgt.un
ldarg.0
ldarg.2
clt.un
and
ret
}
}