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

简介: 一.OpCodes.Ldind_Ref OpCodes.Ldind_Ref ,MSDN的解释是:将对象引用作为 O(对象引用)类型间接加载到计算堆栈上。      比较拗口,我对OpCodes.Ldind_Ref 的理解是,当前计算堆栈顶部的值是一个(对象引用的)地址(即指针的指针),而OpCodes.Ldind_Ref 就是要把这个地址处的对象引用加载到计算堆栈上。

一.OpCodes.Ldind_Ref

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

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

 

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

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

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

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

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

    ldarg. 1  
    ldind.
ref  
    callvirt instance 
string  [mscorlib]System.Object::ToString()

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

 

 二.OpCodes.Ldind_I

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

    ldarg.2  
    ldind.
i4      

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

         ///   <summary>
        
///  Ldind 间接加载。(即从地址加载)
         
///   </summary>        
         public   static   void  Ldind(ILGenerator ilGenerator, Type type)
        {
            
if  ( ! type.IsValueType)
            {
                ilGenerator.Emit(OpCodes.Ldind_Ref);
                
return ;
            }

            
if  (type.IsEnum)
            {
                Type underType 
=  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( " The target type:{0} is not supported by EmitHelper.Ldind() "  ,type));
        } 

 

三.OpCodes.Stind_Ref 与 OpCodes.Stind_I*

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

 

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

  2. 将值推送到堆栈上。

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

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

         ///   <summary>
        
///  Stind 间接存储
         
///   </summary>       
         public   static   void  Stind(ILGenerator ilGenerator, Type type)
        {
            
if  ( ! type.IsValueType)
            {
                ilGenerator.Emit(OpCodes.Stind_Ref);
                
return ;
            }

            
if  (type.IsEnum)
            {
                Type underType 
=  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( " The target type:{0} is not supported by EmitHelper.Stind_ForValueType() " , type));
        } 

 

 

 

目录
相关文章
|
6天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
17天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1320 7
|
5天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
296 129
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
4天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
16天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1388 87
|
4天前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
5天前
|
人工智能 Java API
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
本文介绍AI大模型的核心概念、分类及开发者学习路径,重点讲解如何选择与接入大模型。项目基于Spring Boot,使用阿里云灵积模型(Qwen-Plus),对比SDK、HTTP、Spring AI和LangChain4j四种接入方式,助力开发者高效构建AI应用。
276 122
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
|
5天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
231 82
2025年阿里云域名备案流程(新手图文详细流程)