Ejemplo n.º 1
0
        public void Parse_Test()
        {
            var code = @"class A
             {
                void Add()
                {
                    int a = 1;
                    int b = 2;
                    int c = a + b;
                }
            }";
            var pe   = new MemoryStream();
            var pdb  = new MemoryStream();

            new CSharpService().Compile(code, pe, pdb);

            var types = _peService.ParseTypes(pe, pdb);

            Assert.AreEqual("A", types[0].Name);
            Assert.AreEqual("Add", types[0].Methods[0].Name);
            Assert.AreEqual(4, types[0].Methods[0].BodyLines[0].Line);
            Assert.AreEqual("IL_0000: nop", types[0].Methods[0].BodyLines[0].Instructions[0]);
        }
Ejemplo n.º 2
0
        public ActionResult Parse(string csCode)
        {
            if (_cSharpService.IsValid(csCode))
            {
                List <PEType> peTypes;
                using (Stream peStream = new MemoryStream(), pdbStream = new MemoryStream())
                {
                    _cSharpService.Compile(csCode, peStream, pdbStream);
                    peTypes = _peService.ParseTypes(peStream, pdbStream);
                }

                var csharpTypes = _cSharpService.ParseTypes(csCode);
                var model       = csharpTypes.Select(d => new TypeViewModel
                {
                    Name    = d.Name,
                    Lines   = d.Lines,
                    Methods = d.Methods.Select(m => new Method
                    {
                        Name      = m.Name,
                        Lines     = m.Lines,
                        BodyLines =
                            peTypes.First(i => i.Name == d.Name)
                            .Methods.First(pem => pem.Name == m.Name)
                            .BodyLines.Select(b => new BodyLine
                        {
                            Line         = b.Line,
                            Instructions = b.Instructions
                        })
                    })
                });

                return(Json(model));
            }

            return(Json("error"));
        }