3rdparty/NRefactory/ICSharpCode.NRefactory.Cecil/CecilLoader.cs
/// <summary>
/// Loads the module definition into a project content.
/// </summary>
/// <returns>Unresolved type system representing the assembly</returns>
Loads the module definition into a project content.
Loads the module definition into a project content .
[CLSCompliant(false)]
		public IUnresolvedAssembly LoadModule(ModuleDefinition moduleDefinition)
		{
			if (moduleDefinition == null)
				throw new ArgumentNullException("moduleDefinition");
			this.currentModule = moduleDefinition;
			IList<IUnresolvedAttribute> assemblyAttributes = new List<IUnresolvedAttribute>();
			IList<IUnresolvedAttribute> moduleAttributes = new List<IUnresolvedAttribute>();
			AssemblyDefinition assemblyDefinition = moduleDefinition.Assembly;
			if (assemblyDefinition != null) {
				AddAttributes(assemblyDefinition, assemblyAttributes);
			}
			AddAttributes(moduleDefinition, moduleAttributes);
			assemblyAttributes = interningProvider.InternList(assemblyAttributes);
			moduleAttributes = interningProvider.InternList(moduleAttributes);
			this.currentAssembly = new CecilUnresolvedAssembly(assemblyDefinition != null ? assemblyDefinition.Name.FullName : moduleDefinition.Name, this.DocumentationProvider);
			currentAssembly.Location = moduleDefinition.FullyQualifiedName;
			currentAssembly.AssemblyAttributes.AddRange(assemblyAttributes);
			currentAssembly.ModuleAttributes.AddRange(assemblyAttributes);
			foreach (ExportedType type in moduleDefinition.ExportedTypes) {
				if (type.IsForwarder) {
					int typeParameterCount;
					string ns = type.Namespace;
					string name = ReflectionHelper.SplitTypeParameterCountFromReflectionName(type.Name, out typeParameterCount);
					ns = interningProvider.Intern(ns);
					name = interningProvider.Intern(name);
					var typeRef = new GetClassTypeReference(GetAssemblyReference(type.Scope), ns, name, typeParameterCount);
					typeRef = interningProvider.Intern(typeRef);
					var key = new TopLevelTypeName(ns, name, typeParameterCount);
					currentAssembly.AddTypeForwarder(key, typeRef);
				}
			}
			CecilLoader cecilLoaderCloneForLazyLoading = LazyLoad ? new CecilLoader(this) : null;
			List<TypeDefinition> cecilTypeDefs = new List<TypeDefinition>();
			List<DefaultUnresolvedTypeDefinition> typeDefs = new List<DefaultUnresolvedTypeDefinition>();
			foreach (TypeDefinition td in moduleDefinition.Types) {
				this.CancellationToken.ThrowIfCancellationRequested();
				if (this.IncludeInternalMembers || (td.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public) {
					string name = td.Name;
					if (name.Length == 0)
						continue;
					if (this.LazyLoad) {
						var t = new LazyCecilTypeDefinition(cecilLoaderCloneForLazyLoading, td);
						currentAssembly.AddTypeDefinition(t);
						RegisterCecilObject(t, td);
					} else {
						var t = CreateTopLevelTypeDefinition(td);
						cecilTypeDefs.Add(td);
						typeDefs.Add(t);
						currentAssembly.AddTypeDefinition(t);
					}
				}
			}
			for (int i = 0; i < typeDefs.Count; i++) {
				InitTypeDefinition(cecilTypeDefs[i], typeDefs[i]);
			}
			AddToTypeSystemTranslationTable(this.currentAssembly, assemblyDefinition);
			currentAssembly.Freeze();
			var result = this.currentAssembly;
			this.currentAssembly = null;
			this.currentModule = null;
			return result;
		}
[ CLSCompliant ( false ) ] public IUnresolvedAssembly LoadModule ( ModuleDefinition moduleDefinition ) { if ( moduleDefinition == null ) throw new ArgumentNullException ( " " ) ; this . currentModule = moduleDefinition ; IList < IUnresolvedAttribute > assemblyAttributes = new List < IUnresolvedAttribute > ( ) ; IList < IUnresolvedAttribute > moduleAttributes = new List < IUnresolvedAttribute > ( ) ; AssemblyDefinition assemblyDefinition = moduleDefinition . Assembly ; if ( assemblyDefinition != null ) { AddAttributes ( assemblyDefinition , assemblyAttributes ) ; } AddAttributes ( moduleDefinition , moduleAttributes ) ; assemblyAttributes = interningProvider . InternList ( assemblyAttributes ) ; moduleAttributes = interningProvider . InternList ( moduleAttributes ) ; this . currentAssembly = new CecilUnresolvedAssembly ( assemblyDefinition != null ? assemblyDefinition . Name . FullName : moduleDefinition . Name , this . DocumentationProvider ) ; currentAssembly . Location = moduleDefinition . FullyQualifiedName ; currentAssembly . AssemblyAttributes . AddRange ( assemblyAttributes ) ; currentAssembly . ModuleAttributes . AddRange ( assemblyAttributes ) ; foreach ( ExportedType type in moduleDefinition . ExportedTypes ) { if ( type . IsForwarder ) { int typeParameterCount ; string ns = type . Namespace ; string name = ReflectionHelper . SplitTypeParameterCountFromReflectionName ( type . Name , out typeParameterCount ) ; ns = interningProvider . Intern ( ns ) ; name = interningProvider . Intern ( name ) ; var typeRef = new GetClassTypeReference ( GetAssemblyReference ( type . Scope ) , ns , name , typeParameterCount ) ; typeRef = interningProvider . Intern ( typeRef ) ; var key = new TopLevelTypeName ( ns , name , typeParameterCount ) ; currentAssembly . AddTypeForwarder ( key , typeRef ) ; } } CecilLoader cecilLoaderCloneForLazyLoading = LazyLoad ? new CecilLoader ( this ) : null ; List < TypeDefinition > cecilTypeDefs = new List < TypeDefinition > ( ) ; List < DefaultUnresolvedTypeDefinition > typeDefs = new List < DefaultUnresolvedTypeDefinition > ( ) ; foreach ( TypeDefinition td in moduleDefinition . Types ) { this . CancellationToken . ThrowIfCancellationRequested ( ) ; if ( this . IncludeInternalMembers || ( td . Attributes & TypeAttributes . VisibilityMask ) == TypeAttributes . Public ) { string name = td . Name ; if ( name . Length == 0 ) continue ; if ( this . LazyLoad ) { var t = new LazyCecilTypeDefinition ( cecilLoaderCloneForLazyLoading , td ) ; currentAssembly . AddTypeDefinition ( t ) ; RegisterCecilObject ( t , td ) ; } else { var t = CreateTopLevelTypeDefinition ( td ) ; cecilTypeDefs . Add ( td ) ; typeDefs . Add ( t ) ; currentAssembly . AddTypeDefinition ( t ) ; } } } for ( int i = 0 ; i < typeDefs . Count ; i ++ ) { InitTypeDefinition ( cecilTypeDefs [ i ] , typeDefs [ i ] ) ; } AddToTypeSystemTranslationTable ( this . currentAssembly , assemblyDefinition ) ; currentAssembly . Freeze ( ) ; var result = this . currentAssembly ; this . currentAssembly = null ; this . currentModule = null ; return result ; }
Loads the module definition into a project content.
Loads the module definition into a project content .
// Read assembly and module attributes // Register type forwarders: // Create and register all types: // The registration will happen after the members are initialized // Initialize the type's members: // Freezing the assembly here is important: // otherwise it will be frozen when a compilation is first created // from it. But freezing has the effect of changing some collection instances // (to ReadOnlyCollection). This hidden mutation was causing a crash // when the FastSerializer was saving the assembly at the same time as // the first compilation was created from it. // By freezing the assembly now, we ensure it is usable on multiple // threads without issues.
Unresolved type system representing the assembly
Unresolved type system representing the assembly
moduleDefinition
ModuleDefinition