当前位置: 首页>>代码示例>>C#>>正文


C# ILGen.EmitUnbox方法代码示例

本文整理汇总了C#中ILGen.EmitUnbox方法的典型用法代码示例。如果您正苦于以下问题:C# ILGen.EmitUnbox方法的具体用法?C# ILGen.EmitUnbox怎么用?C# ILGen.EmitUnbox使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ILGen的用法示例。


在下文中一共展示了ILGen.EmitUnbox方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Compile

 public override void Compile(Executive engine, object form, ILGen il, LocalAccess locals, Stack<Type> st, object[] args)
 {
     if (args.Length != 2)
         throw new ArgumentException("Unproperly formated cast expression");
     engine.CompileExpr(il, locals, st, args[0]);
     Type type = args[1] as Type;
     if (type == null)
         throw new ArgumentException("Expecting type value");
     Type curr_type = st.Pop();
     if (curr_type.IsValueType)
     {
         if (type == typeof(System.Object))
             il.EmitBoxing(curr_type);
         else if (curr_type != type)
             throw new InvalidCastException();
     }
     else
     {
         il.Emit(OpCodes.Isinst, type);
         il.Emit(OpCodes.Dup);
         Label ok = il.DefineLabel();
         il.Emit(OpCodes.Brtrue_S, ok);
         il.EmitCall(_raiseInvalidCast);
         il.MarkLabel(ok);
         if (type.IsValueType)
             il.EmitUnbox(type);
     }
     st.Push(type);
 }
开发者ID:,项目名称:,代码行数:29,代码来源:

示例2: CompileExpr

 internal void CompileExpr(ILGen il, LocalAccess locals, Stack<Type> st, object lval)
 {
     if (lval == null)
     {
         il.EmitNull();
         st.Push(typeof(System.Object));
     }
     else if (Lisp.IsNode(lval))
     {
         if (lval == Lisp.NIL || lval == Lisp.T)
         {
             if (lval == Lisp.NIL)
                 il.EmitNull();
             else
                 il.Emit(OpCodes.Ldsfld, typeof(RuntimeOps).GetField("True"));
             st.Push(typeof(System.Object));
         }
         else if (lval == Lisp.INST)
         {
             il.Emit(OpCodes.Ldarg_0);
             il.EmitPropertyGet(typeof(CompiledLambda), "Engine");
             st.Push(typeof(Executive));
         }
         else if (lval == Lisp.ARGV)
         {
             il.Emit(OpCodes.Ldarg_2);
             st.Push(typeof(object[]));
         }
         else if (lval == Lisp.MPOOL)
         {
             il.Emit(OpCodes.Ldarg_3);
             st.Push(typeof(MemoryPool));
         }
         else
         {
             LocalBuilder localVar = locals.GetLocal(lval);
             if (localVar != null)
             {
                 il.Emit(OpCodes.Ldloc, localVar);
                 st.Push(localVar.LocalType);
             }
             else
             {
                 Type type = lval.GetType();
                 if (type == typeof(Integer))
                 {
                     il.EmitDecimal((Decimal)((Integer)lval));
                     il.EmitNew(typeof(Integer).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null,
                         new Type[] { typeof(Decimal) }, null));
                 }
                 else if (!il.TryEmitConstant(lval, type))
                 {
                     il.Emit(OpCodes.Ldarg_1);
                     il.EmitInt(locals.DefineConstant(lval));
                     il.Emit(OpCodes.Ldelem_Ref);
                     if (type.IsValueType)
                         il.EmitUnbox(type);
                 }
                 st.Push(type);
             }
         }
     }
     else if (Lisp.IsFunctor(lval))
     {
         object head = Lisp.Car(lval);
         object[] args = Lisp.ToArray(Lisp.Cdr(lval));
         ControlFormBase control;
         if (m_control.TryGetValue(head, out control))
             control.Compile(this, lval, il, locals, st, args);
         else
         {
             foreach (object a in args)
                 CompileExpr(il, locals, st, a);
             Type[] parameterTypes = new Type[args.Length];
             for (int k = args.Length - 1; k >= 0; k--)
                 parameterTypes[k] = st.Pop();
             FuncName name = new FuncName(head, parameterTypes);
             FuncBase body = GetFunc(name, false);
             if (body == null)
             {
                 bool successed = false;
                 if (parameterTypes.Length == 2)
                 {
                     Type castType = ValueProxy.GetType(parameterTypes[0], parameterTypes[1]);
                     ValueConverter converter1 = FindConverter(parameterTypes[0], castType);
                     ValueConverter converter2 = FindConverter(parameterTypes[1], castType);
                     body = GetFunc(new FuncName(name.ID, new Type[] { castType, castType }), false);
                     if (body != null && converter1 != null && converter2 != null)
                     {
                         LocalBuilder localVar = il.DeclareLocal(parameterTypes[1]);
                         il.Emit(OpCodes.Stloc, localVar);
                         converter1.Compile(this, il, locals, parameterTypes[0]);
                         il.Emit(OpCodes.Ldloc, localVar);
                         il.FreeLocal(localVar);
                         converter2.Compile(this, il, locals, parameterTypes[1]);
                         successed = true;
                     }
                 }
                 if (!successed)
                 {
//.........这里部分代码省略.........
开发者ID:,项目名称:,代码行数:101,代码来源:

示例3: WriteEpilog

 private void WriteEpilog(ILGen il, Parameter[] parameters, LocalAccess locals)
 {
     if (locals.HasLocals || parameters.Length > 0)
     {
         Label label = il.DefineLabel();
         if (parameters.Length > 0)
         {
             for (int k = 0; k < parameters.Length; k++)
             {
                 il.Emit(OpCodes.Ldarg_2);
                 il.EmitInt(k);
                 il.Emit(OpCodes.Ldelem_Ref);
                 if (parameters[k].Type.IsValueType)
                 {
                     il.Emit(OpCodes.Isinst, parameters[k].Type);
                     il.Emit(OpCodes.Dup);
                     il.Emit(OpCodes.Brfalse, label);
                     if (locals.IsParameterBinded(parameters[k].ID))
                     {
                         LocalBuilder local = locals.GetLocal(parameters[k].ID);
                         il.EmitUnbox(local.LocalType);
                         il.Emit(OpCodes.Stloc, local);
                     }
                     else
                         il.Emit(OpCodes.Pop);
                 }
                 else
                 {
                     Label label2 = il.DefineLabel();
                     il.Emit(OpCodes.Dup);
                     il.Emit(OpCodes.Brfalse_S, label2);
                     il.Emit(OpCodes.Isinst, parameters[k].Type);
                     il.Emit(OpCodes.Dup);
                     il.Emit(OpCodes.Brfalse, label);
                     il.MarkLabel(label2);
                     if (locals.IsParameterBinded(parameters[k].ID))
                     {                                                                
                         LocalBuilder local = locals.GetLocal(parameters[k].ID);
                         il.Emit(OpCodes.Stloc, local);
                     }
                     else
                         il.Emit(OpCodes.Pop);
                 }
             }
         }
         if (locals.HasLocals)
         {
             il.Emit(OpCodes.Ldarg_0);
             il.EmitPropertyGet(typeof(CompiledLambda), "Values");
             LocalBuilder values = il.DeclareLocal(typeof(SymbolLink[]));
             il.Emit(OpCodes.Stloc, values);
             int k = 0;
             foreach (LocalAccess.LocalBinding b in locals)
             {
                 il.Emit(OpCodes.Ldarg_3);                        
                 il.Emit(OpCodes.Ldloc, values);
                 il.EmitInt(k++);
                 il.Emit(OpCodes.Ldelem_Ref);
                 il.EmitCall(m_memoryPoolGetData);
                 if (b.Local.LocalType.IsValueType)
                 {
                     il.Emit(OpCodes.Isinst, b.Local.LocalType);
                     il.Emit(OpCodes.Dup);
                     il.Emit(OpCodes.Brfalse, label);
                     il.EmitUnbox(b.Local.LocalType);
                     il.Emit(OpCodes.Stloc, b.Local);
                 }
                 else
                 {
                     Label label2 = il.DefineLabel();
                     il.Emit(OpCodes.Dup);
                     il.Emit(OpCodes.Brfalse_S, label2);
                     il.Emit(OpCodes.Isinst, b.Local.LocalType);
                     il.Emit(OpCodes.Dup);
                     il.Emit(OpCodes.Brfalse, label);
                     il.MarkLabel(label2);
                     il.Emit(OpCodes.Stloc, b.Local);
                 }
             }
             il.FreeLocal(values);
         }
         Label end = il.DefineLabel();
         il.Emit(OpCodes.Br_S, end);
         il.MarkLabel(label);
         il.Emit(OpCodes.Pop); // Isinst
         il.EmitPropertyGet(typeof(Undefined), "Value");
         il.Emit(OpCodes.Ret);
         il.MarkLabel(end);                
     }
 }
开发者ID:,项目名称:,代码行数:90,代码来源:

示例4: GenerateStatics

        protected override void GenerateStatics(GenContext context)
        {
            if (IsDefType)
            {
                TypeBuilder tb = TypeBuilder;

                // getBasis()
                MethodBuilder mbg = tb.DefineMethod("getBasis", MethodAttributes.Public | MethodAttributes.Static, typeof(IPersistentVector), Type.EmptyTypes);
                LambdaExpression lambda = Expression.Lambda(GenerateValue(_hintedFields));
                lambda.CompileToMethod(mbg, context.IsDebuggable);

                if (_fields.count() > _hintedFields.count())
                {
                    // create(IPersistentMap)
                    MethodBuilder mbc = tb.DefineMethod("create", MethodAttributes.Public | MethodAttributes.Static, tb, new Type[] { typeof(IPersistentMap) });
                    ILGen gen = new ILGen(mbc.GetILGenerator());

                    LocalBuilder kwLocal = gen.DeclareLocal(typeof(Keyword));
                    List<LocalBuilder> locals = new List<LocalBuilder>();
                    for (ISeq s = RT.seq(_hintedFields); s != null; s = s.next())
                    {
                        string bName = ((Symbol)s.first()).Name;
                        Type t = Compiler.TagType(Compiler.TagOf(s.first()));

                        // local_kw = Keyword.intern(bname)
                        // local_i = arg_0.valAt(kw,null)
                        gen.EmitLoadArg(0);
                        gen.EmitString(bName);
                        gen.EmitCall(Compiler.Method_Keyword_intern_string);
                        gen.Emit(OpCodes.Dup);
                        gen.Emit(OpCodes.Stloc,kwLocal.LocalIndex);
                        gen.EmitNull();
                        gen.EmitCall(Compiler.Method_IPersistentMap_valAt2);
                        LocalBuilder lb = gen.DeclareLocal(t);
                        locals.Add(lb);
                        if ( t.IsPrimitive )
                            gen.EmitUnbox(t);
                        gen.Emit(OpCodes.Stloc, lb.LocalIndex);

                        // arg_0 = arg_0.without(local_kw);
                        gen.EmitLoadArg(0);
                        gen.Emit(OpCodes.Ldloc,kwLocal.LocalIndex);
                        gen.EmitCall(Compiler.Method_IPersistentMap_without);
                        gen.EmitStoreArg(0);
                    }

                    foreach (LocalBuilder lb in locals)
                        gen.Emit(OpCodes.Ldloc, lb.LocalIndex);
                    gen.EmitNull();
                    gen.EmitLoadArg(0);
                    gen.EmitCall(Compiler.Method_RT_seqOrElse);
                    gen.EmitNew(CtorInfo);

                    gen.Emit(OpCodes.Ret);
                }
            }
        }
开发者ID:richhickey,项目名称:clojure-clr,代码行数:57,代码来源:NewInstanceExpr.cs


注:本文中的ILGen.EmitUnbox方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。