Browse Source

fix #557 - Missing namespaces when decompiling only a method

pull/569/head
Siegfried Pammer 11 years ago
parent
commit
5bb6e4b85b
  1. 23
      ICSharpCode.Decompiler/Ast/Transforms/IntroduceUsingDeclarations.cs

23
ICSharpCode.Decompiler/Ast/Transforms/IntroduceUsingDeclarations.cs

@ -38,24 +38,23 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
public void Run(AstNode compilationUnit) public void Run(AstNode compilationUnit)
{ {
if (!context.Settings.UsingDeclarations)
return;
// First determine all the namespaces that need to be imported: // First determine all the namespaces that need to be imported:
compilationUnit.AcceptVisitor(new FindRequiredImports(this), null); compilationUnit.AcceptVisitor(new FindRequiredImports(this), null);
importedNamespaces.Add("System"); // always import System, even when not necessary importedNamespaces.Add("System"); // always import System, even when not necessary
// Now add using declarations for those namespaces: if (context.Settings.UsingDeclarations) {
foreach (string ns in importedNamespaces.OrderByDescending(n => n)) { // Now add using declarations for those namespaces:
// we go backwards (OrderByDescending) through the list of namespaces because we insert them backwards foreach (string ns in importedNamespaces.OrderByDescending(n => n)) {
// (always inserting at the start of the list) // we go backwards (OrderByDescending) through the list of namespaces because we insert them backwards
string[] parts = ns.Split('.'); // (always inserting at the start of the list)
AstType nsType = new SimpleType(parts[0]); string[] parts = ns.Split('.');
for (int i = 1; i < parts.Length; i++) { AstType nsType = new SimpleType(parts[0]);
nsType = new MemberType { Target = nsType, MemberName = parts[i] }; for (int i = 1; i < parts.Length; i++) {
nsType = new MemberType { Target = nsType, MemberName = parts[i] };
}
compilationUnit.InsertChildAfter(null, new UsingDeclaration { Import = nsType }, SyntaxTree.MemberRole);
} }
compilationUnit.InsertChildAfter(null, new UsingDeclaration { Import = nsType }, SyntaxTree.MemberRole);
} }
if (!context.Settings.FullyQualifyAmbiguousTypeNames) if (!context.Settings.FullyQualifyAmbiguousTypeNames)

Loading…
Cancel
Save