示例#1
0
        public static void emit(System.Reflection.Emit.OpCode OpCode, object Data, System.Reflection.Emit.ILGenerator Generator)
        {
            if (OpCode.FlowControl == System.Reflection.Emit.FlowControl.Branch || OpCode.FlowControl == System.Reflection.Emit.FlowControl.Cond_Branch)
            {
                if (Data is System.Reflection.Emit.Label)
                {
                    Generator.Emit(OpCode, ((System.Reflection.Emit.Label)Data));
                }
                else
                {
                    throw new Exception("Invalid data. The target of a branch has to substituted by a label before beeing emitted");
                }
            }
            else if (OpCode.FlowControl == System.Reflection.Emit.FlowControl.Call)
            {
                Generator.EmitCall(OpCode, Data as System.Reflection.MethodInfo, null);
            }
            else
            {
                if (Data is System.Reflection.LocalVariableInfo)
                {
                    System.Reflection.LocalVariableInfo info = Data as System.Reflection.LocalVariableInfo;

                    if (OpCode == System.Reflection.Emit.OpCodes.Ldloc ||
                        OpCode == System.Reflection.Emit.OpCodes.Ldloc_S ||
                        OpCode == System.Reflection.Emit.OpCodes.Ldloc_0 ||
                        OpCode == System.Reflection.Emit.OpCodes.Ldloc_1 ||
                        OpCode == System.Reflection.Emit.OpCodes.Ldloc_2 ||
                        OpCode == System.Reflection.Emit.OpCodes.Ldloc_3)
                    {
                        switch (info.LocalIndex)
                        {
                        case 0: Generator.Emit(System.Reflection.Emit.OpCodes.Ldloc_0); break;

                        case 1: Generator.Emit(System.Reflection.Emit.OpCodes.Ldloc_1); break;

                        case 2: Generator.Emit(System.Reflection.Emit.OpCodes.Ldloc_2); break;

                        case 3: Generator.Emit(System.Reflection.Emit.OpCodes.Ldloc_3); break;

                        default:
                            if (info.LocalIndex < 256)
                            {
                                Generator.Emit(System.Reflection.Emit.OpCodes.Ldloc_S, (byte)info.LocalIndex);
                            }
                            else
                            {
                                Generator.Emit(System.Reflection.Emit.OpCodes.Ldloc, info.LocalIndex);
                            }
                            break;
                        }
                    }
                    else if (OpCode == System.Reflection.Emit.OpCodes.Stloc ||
                             OpCode == System.Reflection.Emit.OpCodes.Stloc_S ||
                             OpCode == System.Reflection.Emit.OpCodes.Stloc_0 ||
                             OpCode == System.Reflection.Emit.OpCodes.Stloc_1 ||
                             OpCode == System.Reflection.Emit.OpCodes.Stloc_2 ||
                             OpCode == System.Reflection.Emit.OpCodes.Stloc_3)
                    {
                        switch (info.LocalIndex)
                        {
                        case 0: Generator.Emit(System.Reflection.Emit.OpCodes.Stloc_0); break;

                        case 1: Generator.Emit(System.Reflection.Emit.OpCodes.Stloc_1); break;

                        case 2: Generator.Emit(System.Reflection.Emit.OpCodes.Stloc_2); break;

                        case 3: Generator.Emit(System.Reflection.Emit.OpCodes.Stloc_3); break;

                        default:
                            if (info.LocalIndex < 256)
                            {
                                Generator.Emit(System.Reflection.Emit.OpCodes.Stloc_S, (byte)info.LocalIndex);
                            }
                            else
                            {
                                Generator.Emit(System.Reflection.Emit.OpCodes.Stloc, info.LocalIndex);
                            }
                            break;
                        }
                    }
                    else
                    {
                        throw new InvalidProgramException("Invalid OpCode and Data combinaison: " + OpCode.ToString() + " " + Data.ToString());
                    }
                }
                else if (OpCode == System.Reflection.Emit.OpCodes.Ldarg_0 ||
                         OpCode == System.Reflection.Emit.OpCodes.Ldarg_1 ||
                         OpCode == System.Reflection.Emit.OpCodes.Ldarg_2 ||
                         OpCode == System.Reflection.Emit.OpCodes.Ldarg_3 ||
                         OpCode == System.Reflection.Emit.OpCodes.Ldarg_S)
                {
                    int argIndx = (int)Data;
                    switch (argIndx)
                    {
                    case 0: Generator.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); break;

                    case 1: Generator.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); break;

                    case 2: Generator.Emit(System.Reflection.Emit.OpCodes.Ldarg_2); break;

                    case 3: Generator.Emit(System.Reflection.Emit.OpCodes.Ldarg_3); break;

                    default:
                        if (argIndx < 256)
                        {
                            Generator.Emit(System.Reflection.Emit.OpCodes.Ldarg_S, (byte)argIndx);
                        }
                        else
                        {
                            Generator.Emit(System.Reflection.Emit.OpCodes.Ldarg, argIndx);
                        }
                        break;
                    }
                }
                else
                {
                    if (Data == null)
                    {
                        Generator.Emit(OpCode);
                    }
                    else if (Data is int)
                    {
                        Generator.Emit(OpCode, (int)Data);
                    }
                    else if (Data is byte)
                    {
                        Generator.Emit(OpCode, (byte)Data);
                    }
                    else if (Data is double)
                    {
                        Generator.Emit(OpCode, (double)Data);
                    }
                    else if (Data is float)
                    {
                        Generator.Emit(OpCode, (float)Data);
                    }
                    else if (Data is FieldInfo)
                    {
                        Generator.Emit(OpCode, (FieldInfo)Data);
                    }
                    else if (Data is long)
                    {
                        Generator.Emit(OpCode, (long)Data);
                    }
                    else if (Data is Type)
                    {
                        Generator.Emit(OpCode, (Type)Data);
                    }
                    else if (Data is ConstructorInfo)
                    {
                        Generator.Emit(OpCode, (ConstructorInfo)Data);
                    }
                    else
                    {
                        throw new InvalidProgramException("Invalid OpCode and Data combinaison: " + OpCode.ToString() + " " + Data.ToString());
                    }
                }
            }
        }