1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 
6 using System;
7 using System.Collections.Generic;
8 using System.Diagnostics;
9 using System.Runtime;
10 using System.Text;
11 using System.Threading;
12 using Internal.Runtime.Augments;
13 using Internal.Reflection.Execution;
14 using System.Reflection.Metadata;
15 using System.Reflection.PortableExecutable;
16 
17 namespace Internal.Runtime.TypeLoader
18 {
19     public sealed class EcmaModuleInfo : ModuleInfo
20     {
21         /// <summary>
22         /// Metadata Reader for this module.
23         /// </summary>
24         public readonly MetadataReader MetadataReader;
25 
26         /// <summary>
27         /// Ecma PE data for this module.
28         /// </summary>
29         public readonly PEReader PE;
30 
31         /// <summary>
32         /// Initialize module info and construct per-module metadata reader.
33         /// </summary>
34         /// <param name="moduleHandle">Handle (address) of module to initialize</param>
EcmaModuleInfo(TypeManagerHandle moduleHandle, PEReader pe, MetadataReader reader)35         internal EcmaModuleInfo(TypeManagerHandle moduleHandle, PEReader pe, MetadataReader reader)
36             : base(moduleHandle, ModuleType.Ecma)
37         {
38             PE = pe;
39             MetadataReader = reader;
40         }
41     }
42 
43     public static class EcmaModuleList
44     {
45         /// <summary>
46         /// Locate the containing module for a given metadata reader. Assert when not found.
47         /// </summary>
48         /// <param name="reader">Metadata reader to look up</param>
49         /// <returns>Module handle of the module containing the given reader</returns>
GetModuleInfoForMetadataReader(this ModuleList moduleList, MetadataReader reader)50         public static EcmaModuleInfo GetModuleInfoForMetadataReader(this ModuleList moduleList, MetadataReader reader)
51         {
52             foreach (ModuleInfo moduleInfo in moduleList.GetLoadedModuleMapInternal().Modules)
53             {
54                 EcmaModuleInfo ecmaModuleInfo = moduleInfo as EcmaModuleInfo;
55                 if (ecmaModuleInfo == null)
56                     continue;
57 
58                 if (ecmaModuleInfo.MetadataReader == reader)
59                 {
60                     return ecmaModuleInfo;
61                 }
62             }
63 
64             // We should never have a reader that is not associated with a module (where does it come from?!)
65             Debug.Assert(false);
66             return null;
67         }
68     }
69 }
70