1818
1919using System ;
2020using System . Collections . Generic ;
21+ using System . Collections . Immutable ;
2122using System . Diagnostics ;
2223using System . IO ;
2324using System . Linq ;
3031using ICSharpCode . Decompiler . CSharp . Resolver ;
3132using ICSharpCode . Decompiler . CSharp . Syntax ;
3233using ICSharpCode . Decompiler . CSharp . Transforms ;
34+ using ICSharpCode . Decompiler . CSharp . TypeSystem ;
3335using ICSharpCode . Decompiler . DebugInfo ;
3436using ICSharpCode . Decompiler . Disassembler ;
3537using ICSharpCode . Decompiler . Documentation ;
@@ -518,11 +520,28 @@ IDocumentationProvider CreateDefaultDocumentationProvider()
518520 }
519521 }
520522
521- DecompileRun CreateDecompileRun ( )
523+ DecompileRun CreateDecompileRun ( HashSet < string > namespaces )
522524 {
523- return new DecompileRun ( settings ) {
525+ List < INamespace > resolvedNamespaces = new List < INamespace > ( ) ;
526+ foreach ( var ns in namespaces )
527+ {
528+ var resolvedNamespace = typeSystem . GetNamespaceByFullName ( ns ) ;
529+ if ( resolvedNamespace != null )
530+ {
531+ resolvedNamespaces . Add ( resolvedNamespace ) ;
532+ }
533+ }
534+
535+ ResolvedUsingScope usingScope = new ResolvedUsingScope (
536+ new CSharpTypeResolveContext ( typeSystem . MainModule ) ,
537+ typeSystem . RootNamespace ,
538+ resolvedNamespaces . ToImmutableArray ( )
539+ ) ;
540+
541+ return new DecompileRun ( settings , usingScope ) {
524542 DocumentationProvider = DocumentationProvider ?? CreateDefaultDocumentationProvider ( ) ,
525- CancellationToken = CancellationToken
543+ CancellationToken = CancellationToken ,
544+ Namespaces = namespaces
526545 } ;
527546 }
528547
@@ -554,9 +573,10 @@ string SyntaxTreeToString(SyntaxTree syntaxTree)
554573 public SyntaxTree DecompileModuleAndAssemblyAttributes ( )
555574 {
556575 var decompilationContext = new SimpleTypeResolveContext ( typeSystem . MainModule ) ;
557- DecompileRun decompileRun = CreateDecompileRun ( ) ;
576+ var namespaces = new HashSet < string > ( ) ;
558577 syntaxTree = new SyntaxTree ( ) ;
559- RequiredNamespaceCollector . CollectAttributeNamespaces ( module , decompileRun . Namespaces ) ;
578+ RequiredNamespaceCollector . CollectAttributeNamespaces ( module , namespaces ) ;
579+ DecompileRun decompileRun = CreateDecompileRun ( namespaces ) ;
560580 DoDecompileModuleAndAssemblyAttributes ( decompileRun , decompilationContext , syntaxTree ) ;
561581 RunTransforms ( syntaxTree , decompileRun , decompilationContext ) ;
562582 return syntaxTree ;
@@ -640,9 +660,10 @@ public SyntaxTree DecompileWholeModuleAsSingleFile()
640660 public SyntaxTree DecompileWholeModuleAsSingleFile ( bool sortTypes )
641661 {
642662 var decompilationContext = new SimpleTypeResolveContext ( typeSystem . MainModule ) ;
643- var decompileRun = CreateDecompileRun ( ) ;
644663 syntaxTree = new SyntaxTree ( ) ;
645- RequiredNamespaceCollector . CollectNamespaces ( module , decompileRun . Namespaces ) ;
664+ var namespaces = new HashSet < string > ( ) ;
665+ RequiredNamespaceCollector . CollectNamespaces ( module , namespaces ) ;
666+ var decompileRun = CreateDecompileRun ( namespaces ) ;
646667 DoDecompileModuleAndAssemblyAttributes ( decompileRun , decompilationContext , syntaxTree ) ;
647668 var typeDefs = metadata . GetTopLevelTypeDefinitions ( ) ;
648669 if ( sortTypes )
@@ -662,8 +683,9 @@ public SyntaxTree DecompileWholeModuleAsSingleFile(bool sortTypes)
662683 /// </summary>
663684 public ILTransformContext CreateILTransformContext ( ILFunction function )
664685 {
665- var decompileRun = CreateDecompileRun ( ) ;
666- RequiredNamespaceCollector . CollectNamespaces ( function . Method , module , decompileRun . Namespaces ) ;
686+ var namespaces = new HashSet < string > ( ) ;
687+ RequiredNamespaceCollector . CollectNamespaces ( function . Method , module , namespaces ) ;
688+ var decompileRun = CreateDecompileRun ( namespaces ) ;
667689 return new ILTransformContext ( function , typeSystem , DebugInfoProvider , settings ) {
668690 CancellationToken = CancellationToken ,
669691 DecompileRun = decompileRun
@@ -907,17 +929,17 @@ public SyntaxTree DecompileTypes(IEnumerable<TypeDefinitionHandle> types)
907929 if ( types == null )
908930 throw new ArgumentNullException ( nameof ( types ) ) ;
909931 var decompilationContext = new SimpleTypeResolveContext ( typeSystem . MainModule ) ;
910- var decompileRun = CreateDecompileRun ( ) ;
911932 syntaxTree = new SyntaxTree ( ) ;
912-
933+ var namespaces = new HashSet < string > ( ) ;
913934 foreach ( var type in types )
914935 {
915936 CancellationToken . ThrowIfCancellationRequested ( ) ;
916937 if ( type . IsNil )
917938 throw new ArgumentException ( "types contains null element" ) ;
918- RequiredNamespaceCollector . CollectNamespaces ( type , module , decompileRun . Namespaces ) ;
939+ RequiredNamespaceCollector . CollectNamespaces ( type , module , namespaces ) ;
919940 }
920941
942+ var decompileRun = CreateDecompileRun ( namespaces ) ;
921943 DoDecompileTypes ( types , decompileRun , decompilationContext , syntaxTree ) ;
922944 RunTransforms ( syntaxTree , decompileRun , decompilationContext ) ;
923945 return syntaxTree ;
@@ -949,9 +971,10 @@ public SyntaxTree DecompileType(FullTypeName fullTypeName)
949971 if ( type . ParentModule != typeSystem . MainModule )
950972 throw new NotSupportedException ( $ "Type { fullTypeName } was not found in the module being decompiled, but only in { type . ParentModule . Name } ") ;
951973 var decompilationContext = new SimpleTypeResolveContext ( typeSystem . MainModule ) ;
952- var decompileRun = CreateDecompileRun ( ) ;
974+ var namespaces = new HashSet < string > ( ) ;
953975 syntaxTree = new SyntaxTree ( ) ;
954- RequiredNamespaceCollector . CollectNamespaces ( type . MetadataToken , module , decompileRun . Namespaces ) ;
976+ RequiredNamespaceCollector . CollectNamespaces ( type . MetadataToken , module , namespaces ) ;
977+ var decompileRun = CreateDecompileRun ( namespaces ) ;
955978 DoDecompileTypes ( new [ ] { ( TypeDefinitionHandle ) type . MetadataToken } , decompileRun , decompilationContext , syntaxTree ) ;
956979 RunTransforms ( syntaxTree , decompileRun , decompilationContext ) ;
957980 return syntaxTree ;
@@ -984,13 +1007,14 @@ public SyntaxTree Decompile(IEnumerable<EntityHandle> definitions)
9841007 if ( definitions == null )
9851008 throw new ArgumentNullException ( nameof ( definitions ) ) ;
9861009 syntaxTree = new SyntaxTree ( ) ;
987- var decompileRun = CreateDecompileRun ( ) ;
1010+ var namespaces = new HashSet < string > ( ) ;
9881011 foreach ( var entity in definitions )
9891012 {
9901013 if ( entity . IsNil )
9911014 throw new ArgumentException ( "definitions contains null element" ) ;
992- RequiredNamespaceCollector . CollectNamespaces ( entity , module , decompileRun . Namespaces ) ;
1015+ RequiredNamespaceCollector . CollectNamespaces ( entity , module , namespaces ) ;
9931016 }
1017+ var decompileRun = CreateDecompileRun ( namespaces ) ;
9941018
9951019 bool first = true ;
9961020 ITypeDefinition parentTypeDef = null ;
0 commit comments