diff --git a/ILSpy/LoadedAssembly.cs b/ILSpy/LoadedAssembly.cs index 280ce0e8e..727d7628f 100644 --- a/ILSpy/LoadedAssembly.cs +++ b/ILSpy/LoadedAssembly.cs @@ -146,7 +146,7 @@ namespace ICSharpCode.ILSpy ICompilation typeSystem; /// - /// Gets a type system containing all types from this assembly + primitve types from mscorlib. + /// Gets a type system containing all types from this assembly + primitive types from mscorlib. /// Returns null in case of load errors. /// /// @@ -154,30 +154,34 @@ namespace ICSharpCode.ILSpy /// public ICompilation GetTypeSystemOrNull() { - if (typeSystem != null) - return typeSystem; - var module = GetPEFileOrNull(); - if (module == null) - return null; - return typeSystem = new SimpleCompilation( - module.WithOptions(TypeSystemOptions.Default | TypeSystemOptions.Uncached | TypeSystemOptions.KeepModifiers), - MinimalCorlib.Instance); + return LazyInitializer.EnsureInitialized(ref this.typeSystem, () => { + var module = GetPEFileOrNull(); + if (module == null) + return null; + return new SimpleCompilation( + module.WithOptions(TypeSystemOptions.Default | TypeSystemOptions.Uncached | TypeSystemOptions.KeepModifiers), + MinimalCorlib.Instance); + }); } + readonly object typeSystemWithOptionsLockObj = new object(); ICompilation typeSystemWithOptions; TypeSystemOptions currentTypeSystemOptions; public ICompilation GetTypeSystemOrNull(TypeSystemOptions options) { - if (typeSystemWithOptions != null && options == currentTypeSystemOptions) - return typeSystemWithOptions; - var module = GetPEFileOrNull(); - if (module == null) - return null; - currentTypeSystemOptions = options; - return typeSystemWithOptions = new SimpleCompilation( - module.WithOptions(options | TypeSystemOptions.Uncached | TypeSystemOptions.KeepModifiers), - MinimalCorlib.Instance); + lock (typeSystemWithOptionsLockObj) + { + if (typeSystemWithOptions != null && options == currentTypeSystemOptions) + return typeSystemWithOptions; + var module = GetPEFileOrNull(); + if (module == null) + return null; + currentTypeSystemOptions = options; + return typeSystemWithOptions = new SimpleCompilation( + module.WithOptions(options | TypeSystemOptions.Uncached | TypeSystemOptions.KeepModifiers), + MinimalCorlib.Instance); + } } public AssemblyList AssemblyList => assemblyList;