Exemplo n.º 1
1
        public string DecompileMethod(Type @class, string methodName)
        {
            var type = TargetAssembly.MainModule.GetType(@class.FullName.Replace('+', '/'));
            if (type == null)
                throw new Exception(string.Format("Can not locate type '{0}' in assembly '{1}'", @class.FullName, TargetAssembly.FullName));
            var method = type.Methods.FirstOrDefault(m => m.Name == methodName);
            if (method == null)
                throw new Exception(string.Format("Can not locate method '{0}' in type '{1}'", methodName, type.FullName));

            StringWriter source = new StringWriter();
            AstBuilder decompiler = new AstBuilder(new DecompilerContext() { CurrentType = method.DeclaringType });
            decompiler.AddMethod(method);
            decompiler.GenerateCode(new ConciseTextOutput(source));

            return source.ToString();
        }
Exemplo n.º 2
0
		/// <summary>
		/// Compiles and decompiles a source code.
		/// </summary>
		/// <param name="code">The source code to copile.</param>
		/// <returns>The decompilation result of compiled source code.</returns>
		static string RoundtripCode(string code)
		{
			AssemblyDefinition assembly = Compile(code);
			AstBuilder decompiler = new AstBuilder(new DecompilerContext());
			decompiler.AddAssembly(assembly);
			decompiler.Transform(new Helpers.RemoveCompilerAttribute());
			StringWriter output = new StringWriter();
			decompiler.GenerateCode(new PlainTextOutput(output));
			return output.ToString();
		}
Exemplo n.º 3
0
		static void TestFile(string fileName)
		{
			string code = File.ReadAllText(fileName);
			AssemblyDefinition assembly = Compile(code);
			AstBuilder decompiler = new AstBuilder(new DecompilerContext());
			decompiler.AddAssembly(assembly);
			decompiler.Transform(new Helpers.RemoveCompilerAttribute());
			StringWriter output = new StringWriter();
			decompiler.GenerateCode(new PlainTextOutput(output));
			StringWriter diff = new StringWriter();
			if (!Compare(code, output.ToString(), diff)) {
				throw new Exception("Test failure." + Environment.NewLine + diff.ToString());
			}
		}