【Emit基础】OpCodes.Ldind_Ref 和 OpCodes.Ldind_I*

一.OpCodes.Ldind_Ref

OpCodes.Ldind_Ref ,MSDN的解释是:将对象引用作为 O(对象引用)类型间接加载到计算堆栈上。

比较拗口,我对OpCodes.Ldind_Ref 的理解是,当前计算堆栈顶部的值是一个(对象引用的)地址(即指针的指针),而OpCodes.Ldind_Ref 就是要把这个地址处的对象引用加载到计算堆栈上。具体过程如下:

  1. 将地址推送到堆栈上。

  2. 从堆栈中弹出地址;获取位于此地址的对象引用。

  3. 将获取的引用推送到堆栈上。

什么时候用到它了?通常是Emit加载方法的out/ref参数时。举个例子:

我们需要Emit一个这样的动作,为方法的第一个参数(自定义引用类型,使用out修饰)调用ToString方法:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> ldarg. 1
ldind.
ref
callvirtinstance
string [mscorlib]System.Object::ToString()

由于第一个参数使用了out修饰符,说明传入方法的实际上是对象引用的地址(即地址的地址),而该地址并不是一个对象引用,所以必须通过Ldind_Ref将该地址处的对象引用加载到计算堆栈,而后才能对其调用ToString()方法。

二.OpCodes.Ldind_I

如果ref/out参数是一个值类型,也是类似的情况,这时我们就需要使用OpCodes.Ldind_I*系列的字段。比如,将一个int类型的out参数间接加载到计算堆栈:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> ldarg.2
ldind.
i4

i4表示目标类型是Int32,而对于不同的值类型,会有不同的操作符字段,使用下面的方法,可以简化调用:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> /// <summary>
/// Ldind间接加载。(即从地址加载)
/// </summary>
public static void Ldind(ILGeneratorilGenerator,Typetype)
{
if ( ! type.IsValueType)
{
ilGenerator.Emit(OpCodes.Ldind_Ref);
return ;
}

if (type.IsEnum)
{
TypeunderType
= Enum.GetUnderlyingType(type);
EmitHelper.Ldind(ilGenerator,underType);
return ;
}

if (type == typeof (Int64))
{
ilGenerator.Emit(OpCodes.Ldind_I8);
return ;
}

if (type == typeof (Int32))
{
ilGenerator.Emit(OpCodes.Ldind_I4);
return ;
}

if (type == typeof (Int16))
{
ilGenerator.Emit(OpCodes.Ldind_I2);
return ;
}

if (type == typeof (Byte))
{
ilGenerator.Emit(OpCodes.Ldind_U1);
return ;
}

if (type == typeof (SByte))
{
ilGenerator.Emit(OpCodes.Ldind_I1);
return ;
}

if (type == typeof (Boolean))
{
ilGenerator.Emit(OpCodes.Ldind_I1);
return ;
}

if (type == typeof (UInt64))
{
ilGenerator.Emit(OpCodes.Ldind_I8);
return ;
}

if (type == typeof (UInt32))
{
ilGenerator.Emit(OpCodes.Ldind_U4);
return ;
}

if (type == typeof (UInt16))
{
ilGenerator.Emit(OpCodes.Ldind_U2);
return ;
}

if (type == typeof (Single))
{
ilGenerator.Emit(OpCodes.Ldind_R4);
return ;
}

if (type == typeof (Double))
{
ilGenerator.Emit(OpCodes.Ldind_R8);
return ;
}

if (type == typeof (System.IntPtr))
{
ilGenerator.Emit(OpCodes.Ldind_I4);
return ;
}

if (type == typeof (System.UIntPtr))
{
ilGenerator.Emit(OpCodes.Ldind_I4);
return ;
}

throw new Exception( string .Format( " Thetargettype:{0}isnotsupportedbyEmitHelper.Ldind() " ,type));
}

三.OpCodes.Stind_Ref 与 OpCodes.Stind_I*

与OpCodes.Ldind_Ref 和 OpCodes.Ldind_I* 的间接加载相反,OpCodes.Stind_Ref 与 OpCodes.Stind_I* 是间接存储,即

  1. 将地址推送到堆栈上。

  2. 将值推送到堆栈上。

  3. 从堆栈中弹出值和地址;将值存储在该地址。

为了简化调用,封装下面的Helper方法:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> /// <summary>
/// Stind间接存储
/// </summary>
public static void Stind(ILGeneratorilGenerator,Typetype)
{
if ( ! type.IsValueType)
{
ilGenerator.Emit(OpCodes.Stind_Ref);
return ;
}

if (type.IsEnum)
{
TypeunderType
= Enum.GetUnderlyingType(type);
EmitHelper.Stind(ilGenerator,underType);
return ;
}

if (type == typeof (Int64))
{
ilGenerator.Emit(OpCodes.Stind_I8);
return ;
}

if (type == typeof (Int32))
{
ilGenerator.Emit(OpCodes.Stind_I4);
return ;
}

if (type == typeof (Int16))
{
ilGenerator.Emit(OpCodes.Stind_I2);
return ;
}

if (type == typeof (Byte))
{
ilGenerator.Emit(OpCodes.Stind_I1);
return ;
}

if (type == typeof (SByte))
{
ilGenerator.Emit(OpCodes.Stind_I1);
return ;
}

if (type == typeof (Boolean))
{
ilGenerator.Emit(OpCodes.Stind_I1);
return ;
}

if (type == typeof (UInt64))
{
ilGenerator.Emit(OpCodes.Stind_I8);
return ;
}

if (type == typeof (UInt32))
{
ilGenerator.Emit(OpCodes.Stind_I4);
return ;
}

if (type == typeof (UInt16))
{
ilGenerator.Emit(OpCodes.Stind_I2);
return ;
}

if (type == typeof (Single))
{
ilGenerator.Emit(OpCodes.Stind_R4);
return ;
}

if (type == typeof (Double))
{
ilGenerator.Emit(OpCodes.Stind_R8);
return ;
}

if (type == typeof (System.IntPtr))
{
ilGenerator.Emit(OpCodes.Stind_I4);
return ;
}

if (type == typeof (System.UIntPtr))
{
ilGenerator.Emit(OpCodes.Stind_I4);
return ;
}

throw new Exception( string .Format( " Thetargettype:{0}isnotsupportedbyEmitHelper.Stind_ForValueType() " ,type));
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值