本文整理汇总了C#中ICSharpCode.Decompiler.Ast.AstBuilder.AddType方法的典型用法代码示例。如果您正苦于以下问题:C# AstBuilder.AddType方法的具体用法?C# AstBuilder.AddType怎么用?C# AstBuilder.AddType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.Decompiler.Ast.AstBuilder
的用法示例。
在下文中一共展示了AstBuilder.AddType方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetClass
public string GetClass(TypeIdentity identity)
{
// Before we attempt to fetch it just try a decompilation.
GetAssembly(identity.AssemblyPath);
ModuleDefinition moduleDef;
if (!this.loadedModules.TryGetValue(identity.AssemblyPath, out moduleDef))
{
// Can't find the assembly, just return nothing.
return string.Empty;
}
TypeDefinition typeDef = moduleDef.GetType(identity.FullyQualifiedName);
if (typeDef == null)
{
// If we can't find our type just return as well.
return string.Empty;
}
DecompilerContext context = new DecompilerContext(moduleDef);
AstBuilder astBuilder = new AstBuilder(context);
astBuilder.AddType(typeDef);
PlainTextOutput textOutput = new PlainTextOutput();
astBuilder.GenerateCode(textOutput);
return textOutput.ToString();
}
示例2: DecompileOnDemand
public void DecompileOnDemand(TypeDefinition type)
{
if (type == null)
return;
if (CheckMappings(type.MetadataToken.ToInt32()))
return;
try {
DecompilerContext context = new DecompilerContext(type.Module);
AstBuilder astBuilder = new AstBuilder(context);
astBuilder.AddType(type);
astBuilder.GenerateCode(new PlainTextOutput());
int token = type.MetadataToken.ToInt32();
var info = new DecompileInformation {
CodeMappings = astBuilder.CodeMappings,
LocalVariables = astBuilder.LocalVariables,
DecompiledMemberReferences = astBuilder.DecompiledMemberReferences
};
// save the data
DebugInformation.AddOrUpdate(token, info, (k, v) => info);
} catch {
return;
}
}
示例3: CreateBuilder
AstBuilder CreateBuilder(IDnlibDef item, CancellationToken token) {
ModuleDef moduleDef;
DecompilerContext ctx;
AstBuilder builder;
if (item is ModuleDef) {
var def = (ModuleDef)item;
moduleDef = def;
builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token });
builder.AddAssembly(def, true);
}
else if (item is TypeDef) {
var def = (TypeDef)item;
moduleDef = def.Module;
builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token });
builder.DecompileMethodBodies = false;
ctx.CurrentType = def;
builder.AddType(def);
}
else if (item is MethodDef) {
var def = (MethodDef)item;
moduleDef = def.Module;
builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token });
ctx.CurrentType = def.DeclaringType;
builder.AddMethod(def);
}
else if (item is FieldDef) {
var def = (FieldDef)item;
moduleDef = def.Module;
builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token });
ctx.CurrentType = def.DeclaringType;
builder.AddField(def);
}
else if (item is PropertyDef) {
var def = (PropertyDef)item;
moduleDef = def.Module;
builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token });
ctx.CurrentType = def.DeclaringType;
builder.AddProperty(def);
}
else if (item is EventDef) {
var def = (EventDef)item;
moduleDef = def.Module;
builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token });
ctx.CurrentType = def.DeclaringType;
builder.AddEvent(def);
}
else
return null;
ctx.Settings = new DecompilerSettings {
UsingDeclarations = false
};
return builder;
}
示例4: DecompileTypeFromModule
private void DecompileTypeFromModule(string typeName, string moduleFileName)
{
var assemblyDef = AssemblyDefinition.ReadAssembly(moduleFileName);
var typeDef = TypeDefFromAssemblyDef(typeName, assemblyDef);
AstBuilder decompiler = new AstBuilder(
new DecompilerContext(typeDef.Module));
decompiler.AddType(typeDef);
GenerateCode(decompiler);
}
示例5: GetConstructors
private List<MethodDefinition> GetConstructors(TypeDefinition type, bool isBaseInitializer)
{
List<MethodDefinition> constructors = new List<MethodDefinition>();
var visitor = new ObservableAstVisitor();
visitor.EnterConstructorDeclaration += exp => {
var constructor = exp.Annotation<MethodReference>();
if(!isBaseInitializer || exp.Initializer.ConstructorInitializerType == ConstructorInitializerType.Base){
constructors.Add(constructor.Resolve());
}
};
ProcessNewInstructions();
var d = new DecompilerContext(type.Module) {CurrentType = type};
var astBuilder = new AstBuilder(d);
astBuilder.AddType(type);
astBuilder.SyntaxTree.AcceptVisitor(visitor);
return constructors;
}
示例6: FixGeneratedType
private void FixGeneratedType()
{
var d = new DecompilerContext(DestinationType.Module);
d.CurrentType = DestinationType;
var visitor = new ObservableAstVisitor();
visitor.EnterInvocationExpression += exp => {
var target = exp.Target;
var methodInvoke = exp.Annotation<MethodReference>();
if(methodInvoke==null)
return;
for(var i = 0; i< exp.Arguments.Count; i++){
var arg = exp.Arguments.ToList()[i];
var argType = arg.Annotation<TypeInformation>().InferredType;
var param = methodInvoke.Parameters[i];
var paramType = param.ParameterType;
if(argType.IsValueType && !paramType.IsValueType){
var argILRange = arg.Annotation<List<ILRange>>();
var insts = GetInstructions(arg);
var inst = GetInstruction(insts, argILRange[0].To);
if(inst.Previous.OpCode.Code != Code.Box){
var box = Instruction.Create(OpCodes.Box,argType);
insts.Insert(insts.IndexOf(inst),box);
Console.WriteLine("Box inserted! Method Arg "+ param.Name + " " +argType + " to " + paramType);
}
}
}
};
ProcessNewInstructions();
var astBuilder = new AstBuilder(d);
astBuilder.AddType(DestinationType);
// astBuilder.RunTransformations();
astBuilder.SyntaxTree.AcceptVisitor(visitor);
}
示例7: CopyConstructorInitInstructions
private void CopyConstructorInitInstructions()
{
var constructors = GetConstructors(DestinationType, true);
var visitor = new ObservableAstVisitor();
bool isNewConstructor = false;
visitor.EnterConstructorDeclaration += exp => {
var constructor = exp.Annotation<MethodReference>();
if(!copier.CopyMap.ContainsKey(constructor)){
isNewConstructor = true;
}
};
visitor.LeaveConstructorDeclaration += exp => {
isNewConstructor = false;
};
visitor.EnterConstructorInitializer += exp => {
isNewConstructor = false;
};
visitor.EnterAssignmentExpression += exp => {
var member = exp.Left.Annotation<MemberReference>();
if(isNewConstructor){
if(copier.CopyMap.ContainsKey(member)){
var argILRange = exp.Annotation<List<ILRange>>();
var newInstructions = GetInstructions(exp);
foreach(var constructor in constructors){
var newInstruction = GetInstruction(newInstructions, argILRange[0].To);
newInstruction = newInstruction.Previous;
while(newInstruction != null && newInstruction.Offset >= argILRange[0].From){
constructor.Body.Instructions.Insert(0, copier.Copy(newInstruction));
newInstruction = newInstruction.Previous;
}
}
Console.WriteLine("Copied to "+constructors.Count+" constructor assigment of "+member);
}
}
};
ProcessNewInstructions();
var d = new DecompilerContext(OriginType.Module) {CurrentType = OriginType};
var astBuilder = new AstBuilder(d);
astBuilder.AddType(OriginType);
astBuilder.SyntaxTree.AcceptVisitor(visitor);
}
示例8: RunDecompiler
void RunDecompiler(string assemblyFile, string fullTypeName, DebuggerTextOutput textOutput, CancellationToken cancellationToken)
{
ReaderParameters readerParameters = new ReaderParameters();
// Use new assembly resolver instance so that the AssemblyDefinitions can be garbage-collected
// once the code is decompiled.
readerParameters.AssemblyResolver = new ILSpyAssemblyResolver(Path.GetDirectoryName(assemblyFile));
ModuleDefinition module = ModuleDefinition.ReadModule(assemblyFile, readerParameters);
TypeDefinition typeDefinition = module.GetType(fullTypeName);
if (typeDefinition == null)
throw new InvalidOperationException("Could not find type");
DecompilerContext context = new DecompilerContext(module);
context.CancellationToken = cancellationToken;
AstBuilder astBuilder = new AstBuilder(context);
astBuilder.AddType(typeDefinition);
astBuilder.GenerateCode(textOutput);
// save decompilation data
decompiledType = typeDefinition;
memberLocations = textOutput.MemberLocations;
}
示例9: RunDecompiler
void RunDecompiler(string assemblyFile, string fullTypeName, ITextOutput textOutput, CancellationToken cancellationToken)
{
ReaderParameters readerParameters = new ReaderParameters();
// Use new assembly resolver instance so that the AssemblyDefinitions can be garbage-collected
// once the code is decompiled.
readerParameters.AssemblyResolver = new ILSpyAssemblyResolver(Path.GetDirectoryName(assemblyFile));
ModuleDefinition module = ModuleDefinition.ReadModule(assemblyFile, readerParameters);
TypeDefinition typeDefinition = module.GetType(fullTypeName);
if (typeDefinition == null)
throw new InvalidOperationException("Could not find type");
DecompilerContext context = new DecompilerContext(module);
context.CancellationToken = cancellationToken;
AstBuilder astBuilder = new AstBuilder(context);
astBuilder.AddType(typeDefinition);
astBuilder.GenerateCode(textOutput);
// save decompilation data
decompiledType = typeDefinition;
/*
int token = decompiledType.MetadataToken.ToInt32();
var info = new DecompileInformation {
CodeMappings = astBuilder.CodeMappings,
LocalVariables = astBuilder.LocalVariables,
DecompiledMemberReferences = astBuilder.DecompiledMemberReferences
};
// save the data
DebuggerDecompilerService.DebugInformation.AddOrUpdate(token, info, (k, v) => info);
*/
}
示例10: Compile
//.........这里部分代码省略.........
rootnode.Accept(new SetNamescopesAndRegisterNamesVisitor(visitorContext), null);
rootnode.Accept(new SetFieldVisitor(visitorContext), null);
rootnode.Accept(new SetResourcesVisitor(visitorContext), null);
rootnode.Accept(new SetPropertiesVisitor(visitorContext, true), null);
il.Emit(OpCodes.Ret);
initComp.Body = body;
}
catch (XamlParseException xpe)
{
LogLine(2, "failed.");
LogError(null, null, null, resource.Name, xpe.XmlInfo.LineNumber, xpe.XmlInfo.LinePosition, 0, 0, xpe.Message,
xpe.HelpLink, xpe.Source);
LogLine(4, xpe.StackTrace);
success = false;
continue;
}
catch (XmlException xe)
{
LogLine(2, "failed.");
LogError(null, null, null, resource.Name, xe.LineNumber, xe.LinePosition, 0, 0, xe.Message, xe.HelpLink, xe.Source);
LogLine(4, xe.StackTrace);
success = false;
continue;
}
catch (Exception e)
{
LogLine(2, "failed.");
LogError(null, null, null, resource.Name, 0, 0, 0, 0, e.Message, e.HelpLink, e.Source);
LogLine(4, e.StackTrace);
success = false;
continue;
}
LogLine(2, "done.");
if (OptimizeIL)
{
Log(2, " Optimizing IL... ");
initComp.Body.OptimizeMacros();
LogLine(2, "done");
}
if (OutputGeneratedILAsCode)
{
var filepath = Path.Combine(Path.GetDirectoryName(Assembly), typeDef.FullName + ".decompiled.cs");
Log(2, " Decompiling {0} into {1}...", typeDef.FullName, filepath);
var decompilerContext = new DecompilerContext(module);
using (var writer = new StreamWriter(filepath))
{
var output = new PlainTextOutput(writer);
var codeDomBuilder = new AstBuilder(decompilerContext);
codeDomBuilder.AddType(typeDef);
codeDomBuilder.GenerateCode(output);
}
LogLine(2, "done");
}
resourcesToPrune.Add(resource);
}
if (!KeepXamlResources)
{
if (resourcesToPrune.Any())
LogLine(2, " Removing compiled xaml resources");
foreach (var resource in resourcesToPrune)
{
Log(2, " Removing {0}... ", resource.Name);
module.Resources.Remove(resource);
LogLine(2, "done");
}
}
LogLine(2, "");
}
if (!hasCompiledXamlResources)
{
LogLine(1, "No compiled resources. Skipping writing assembly.");
return success;
}
Log(1, "Writing the assembly... ");
try
{
assemblyDefinition.Write(Assembly, new WriterParameters
{
WriteSymbols = DebugSymbols
});
LogLine(1, "done.");
}
catch (Exception e)
{
LogLine(1, "failed.");
LogError(null, null, null, null, 0, 0, 0, 0, e.Message, e.HelpLink, e.Source);
LogLine(4, e.StackTrace);
success = false;
}
return success;
}
示例11: Decompile
public ICSharpCode.AvalonEdit.Document.TextDocument Decompile(object obj)
{
AvalonEditTextOutput aeto = new AvalonEditTextOutput();
AstBuilder ast = new AstBuilder(new DecompilerContext(ModuleDefinition.CreateModule("ash", ModuleKind.NetModule)));
switch (obj.GetType().Name)
{
case "AssemblyDefinition":
ast = new AstBuilder(new DecompilerContext((obj as AssemblyDefinition).MainModule) { Settings = new DecompilerSettings() });
try { ast.AddAssembly(obj as AssemblyDefinition); }
catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.FullName); }
break;
case "TypeDefinition":
ast = CreateAstBuilder((obj as TypeDefinition), true);
try { ast.AddType(obj as TypeDefinition); }
catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.FullName); }
break;
case "MethodDefinition":
MethodDefinition method = (obj as MethodDefinition);
ast = CreateAstBuilder(method.DeclaringType, true);
if (method.IsConstructor && !method.IsStatic && !method.DeclaringType.IsValueType)
{
foreach (var field in method.DeclaringType.Fields)
if (field.IsStatic == method.IsStatic)
{
try { ast.AddField(field); }
catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); }
}
foreach (var ctor in method.DeclaringType.Methods)
if (ctor.IsConstructor && ctor.IsStatic == method.IsStatic)
{
try { ast.AddMethod(ctor); }
catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); }
}
}
else
{
try { ast.AddMethod(obj as MethodDefinition); }
catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); }
}
break;
case "PropertyDefinition":
ast = CreateAstBuilder((obj as PropertyDefinition).DeclaringType, true);
try { ast.AddProperty(obj as PropertyDefinition); }
catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); }
break;
case "FieldDefinition":
ast = CreateAstBuilder((obj as FieldDefinition).DeclaringType, true);
try { ast.AddField(obj as FieldDefinition); }
catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); }
break;
case "EventDefinition":
ast = CreateAstBuilder((obj as EventDefinition).DeclaringType, true);
try { ast.AddEvent(obj as EventDefinition); }
catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); }
break;
default:
return new ICSharpCode.AvalonEdit.Document.TextDocument();
}
try { ast.GenerateCode(aeto); }
catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly upon code generation:\r" + e.AssemblyReference.FullName); }
return aeto.GetDocument();
}
示例12: RunDecompiler
void RunDecompiler(string assemblyFile, string fullTypeName, ITextOutput textOutput, CancellationToken cancellationToken)
{
ReaderParameters readerParameters = new ReaderParameters();
// Use new assembly resolver instance so that the AssemblyDefinitions can be garbage-collected
// once the code is decompiled.
readerParameters.AssemblyResolver = new DefaultAssemblyResolver();
ModuleDefinition module = ModuleDefinition.ReadModule(assemblyFile, readerParameters);
TypeDefinition typeDefinition = module.GetType(fullTypeName);
if (typeDefinition == null)
throw new InvalidOperationException("Could not find type");
DecompilerContext context = new DecompilerContext(module);
context.CancellationToken = cancellationToken;
AstBuilder astBuilder = new AstBuilder(context);
astBuilder.AddType(typeDefinition);
astBuilder.GenerateCode(textOutput);
// save decompilation data
var nodes = TreeTraversal
.PreOrder((AstNode)astBuilder.CompilationUnit, n => n.Children)
.Where(n => n is AttributedNode && n.Annotation<Tuple<int, int>>() != null);
MemberReference = typeDefinition;
int token = MemberReference.MetadataToken.ToInt32();
var info = new DecompileInformation {
CodeMappings = astBuilder.CodeMappings,
LocalVariables = astBuilder.LocalVariables,
DecompiledMemberReferences = astBuilder.DecompiledMemberReferences,
AstNodes = nodes
};
// save the data
DebuggerDecompilerService.DebugInformation.AddOrUpdate(token, info, (k, v) => info);
}