示例#1
0
        public void AddVariableIndex()
        {
            var object_ref = new TypeReference ("System", "Object", null, null, false);
            var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
            var body = new MethodBody (method);

            var x = new VariableDefinition ("x", object_ref);
            var y = new VariableDefinition ("y", object_ref);

            body.Variables.Add (x);
            body.Variables.Add (y);

            Assert.AreEqual (0, x.Index);
            Assert.AreEqual (1, y.Index);
        }
示例#2
0
文件: PdbTests.cs 项目: ttRevan/cecil
        public void CreateMethodFromScratch()
        {
            IgnoreOnMono ();

            var module = ModuleDefinition.CreateModule ("Pan", ModuleKind.Dll);
            var type = new TypeDefinition ("Pin", "Pon", TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.Sealed, module.ImportReference (typeof (object)));
            module.Types.Add (type);

            var method = new MethodDefinition ("Pang", MethodAttributes.Public | MethodAttributes.Static, module.ImportReference (typeof (string)));
            type.Methods.Add (method);

            var body = method.Body;

            body.InitLocals = true;

            var il = body.GetILProcessor ();
            var temp = new VariableDefinition ("temp", module.ImportReference (typeof (string)));
            body.Variables.Add (temp);

            il.Emit (OpCodes.Nop);
            il.Emit (OpCodes.Ldstr, "hello");
            il.Emit (OpCodes.Stloc, temp);
            il.Emit (OpCodes.Ldloc, temp);
            il.Emit (OpCodes.Ret);

            body.Instructions [0].SequencePoint = new SequencePoint (new Document (@"C:\test.cs")) {
                StartLine = 0,
                StartColumn = 0,
                EndLine = 0,
                EndColumn = 4,
            };

            var file = Path.Combine (Path.GetTempPath (), "Pan.dll");
            module.Write (file, new WriterParameters {
                SymbolWriterProvider = new PdbWriterProvider (),
            });

            module = ModuleDefinition.ReadModule (file, new ReaderParameters {
                SymbolReaderProvider = new PdbReaderProvider (),
            });

            method = module.GetType ("Pin.Pon").GetMethod ("Pang");

            Assert.AreEqual ("temp", method.Body.Variables [0].Name);
        }
示例#3
0
 public void Emit(OpCode opcode, VariableDefinition variable)
 {
     Append (Create (opcode, variable));
 }
示例#4
0
 public Instruction Create(OpCode opcode, VariableDefinition variable)
 {
     return Instruction.Create (opcode, variable);
 }
示例#5
0
 static int GetVariableIndex(VariableDefinition variable)
 {
     return variable.Index;
 }
示例#6
0
 void CreateLocalVariable(VariableDefinition variable, SymbolToken local_var_token, int start_offset, int end_offset)
 {
     writer.DefineLocalVariable2 (
         variable.Name,
         0,
         local_var_token,
         SymAddressKind.ILOffset,
         variable.Index,
         0,
         0,
         start_offset,
         end_offset);
 }
示例#7
0
        public static Instruction Create(OpCode opcode, VariableDefinition variable)
        {
            if (variable == null)
                throw new ArgumentNullException ("variable");
            if (opcode.OperandType != OperandType.ShortInlineVar &&
                opcode.OperandType != OperandType.InlineVar)
                throw new ArgumentException ("opcode");

            return new Instruction (opcode, variable);
        }