コード例 #1
0
 public static void SetLocalArrayElementValue(ILGenerator il, LocalBuilder arrayLocal, int index, Action emitValue)
 {
     EmitHelper.LoadVar(il, arrayLocal);
     EmitHelper.LoadInt(il, index);
     emitValue();
     il.Emit(OpCodes.Stelem_Ref);
 }
コード例 #2
0
        //public static void CallDynamic(ILGenerator il, MethodInfo method)
        //{
        //il.Emit(OpCodes.Callvirt, method);

        /*if (method.IsAbstract || method.IsVirtual)
         * {
         *  il.Emit(OpCodes.Callvirt, method);
         * }
         * else
         * {
         *  il.Emit(OpCodes.Call, method);
         * }*/
        /* Console.WriteLine(string.Format("{0} IsStatic:{1}", method.Name, method.IsStatic));
         * if (method.IsStatic)
         * {
         *   il.Emit(OpCodes.Call, method);
         * }
         * else
         * {
         *   il.Emit(OpCodes.Callvirt, method);
         * }
         * }*/
        /*
         * public static void CallDynamic(ILGenerator il, MethodInfo method,Type objType)
         * {
         *  if (objType == method.DeclaringType)
         *      il.Emit(OpCodes.Call, method);
         *  else
         *      il.Emit(OpCodes.Callvirt, method);
         * }*/

        public static bool EmitSymbolGet(ILGenerator il, SymbolInfo symbol)
        {
            if (symbol is SymbolVar)
            {
                var symbolVar = symbol as SymbolVar;
                EmitHelper.LoadVar(il, symbolVar.VarBuilder);
            }
            else if (symbol is SymbolArg)
            {
                SymbolArg argsymbol = (symbol as SymbolArg);
                il.Emit(OpCodes.Ldarg, argsymbol.ArgIndex);
            }
            else if (symbol is SymbolEnumItem)
            {
                SymbolEnumItem eisymbol = (symbol as SymbolEnumItem);
                EmitHelper.LoadInt(il, (int)eisymbol.EnumValue);
            }
            else if (symbol is SymbolPropertyDirect)
            {
                SymbolPropertyDirect prsymbol  = symbol as SymbolPropertyDirect;
                MethodInfo           getMethod = prsymbol.ExProperty.Property.GetGetMethod();
                EmitHelper.CallDynamic(il, getMethod, prsymbol.ExProperty.IsSelf);
            }
            else if (symbol is SymbolFieldDirect)
            {
                SymbolFieldDirect fymbol = symbol as SymbolFieldDirect;
                EmitHelper.LoadField(il, fymbol.ExField.Field);
            }
            else if (symbol is SymbolDefProperty)
            {
                SymbolDefProperty prsymbol = (symbol as SymbolDefProperty);
                if (!prsymbol.IsStatic)
                {
                    il.Emit(OpCodes.Ldarg_0);
                }
                MethodInfo getMethod = prsymbol.GetProperty().GetGetMethod();
                EmitHelper.CallDynamic(il, getMethod, true);
            }
            else if (symbol is SymbolDefField)
            {
                SymbolDefField prsymbol = (symbol as SymbolDefField);
                if (!prsymbol.IsStatic)
                {
                    il.Emit(OpCodes.Ldarg_0);
                }
                //il.Emit(OpCodes.Ldfld, prsymbol.Builder);
                EmitHelper.LoadField(il, prsymbol.GetField());
            }
            else if (symbol is SymbolDefClass)
            {
                return(true);
            }
            else
            {
                return(false);
            }
            return(true);
        }
コード例 #3
0
 public static void LoadField(ILGenerator il, FieldInfo field)
 {
     if (field.IsLiteral)
     {
         object value = field.GetValue(null);
         if (value is int)
         {
             EmitHelper.LoadInt(il, (int)value);
         }
         else if (value is float)
         {
             il.Emit(OpCodes.Ldc_R4, (float)value);
         }
         else if (value is string)
         {
             il.Emit(OpCodes.Ldstr, (string)value);
         }
         else if (value is bool)
         {
             bool bv = (bool)value;
             if (bv)
             {
                 il.Emit(OpCodes.Ldc_I4_1);
             }
             else
             {
                 il.Emit(OpCodes.Ldc_I4_0);
             }
         }
         else
         {
             throw new Exception("编译器不支持" + field.FieldType.Name + "类型");
         }
     }
     else if (field.IsStatic)
     {
         il.Emit(OpCodes.Ldsfld, field);
     }
     else
     {
         il.Emit(OpCodes.Ldfld, field);
     }
 }
コード例 #4
0
 public static void EmitLoad(ILGenerator il, SymbolRefStaticMember symbol)
 {
     if (symbol.ZMember is ZPropertyInfo)
     {
         MethodInfo getMethod = (symbol.ZMember as ZPropertyInfo).SharpProperty.GetGetMethod();
         EmitHelper.CallDynamic(il, getMethod);
     }
     else if (symbol.ZMember is ZFieldInfo)
     {
         EmitHelper.LoadField(il, (symbol.ZMember as ZFieldInfo).SharpField);
     }
     else if (symbol.ZMember is ZEnumElementInfo)
     {
         int enumValue = (int)((symbol.ZMember as ZEnumElementInfo).Value);
         EmitHelper.LoadInt(il, enumValue);
     }
     else
     {
         throw new CompileException();
     }
 }