diff --git a/src/coreclr/tools/Common/TypeSystem/Ecma/EcmaModule.cs b/src/coreclr/tools/Common/TypeSystem/Ecma/EcmaModule.cs index 27f4e7ecd56f4..33114a4524c51 100644 --- a/src/coreclr/tools/Common/TypeSystem/Ecma/EcmaModule.cs +++ b/src/coreclr/tools/Common/TypeSystem/Ecma/EcmaModule.cs @@ -8,6 +8,8 @@ using System.Reflection.Metadata.Ecma335; using System.Reflection.PortableExecutable; +using Debug = System.Diagnostics.Debug; + namespace Internal.TypeSystem.Ecma { public partial class EcmaModule : ModuleDesc @@ -376,6 +378,26 @@ public FieldDesc GetField(EntityHandle handle) return field; } + internal EcmaField GetField(FieldDefinitionHandle handle, EcmaType owningType) + { + if (!_resolvedTokens.TryGetValue(handle, out IEntityHandleObject result)) + { + Debug.Assert(_metadataReader.GetFieldDefinition(handle).GetDeclaringType() == owningType.Handle); + result = _resolvedTokens.AddOrGetExisting(new EcmaField(owningType, handle)); + } + return (EcmaField)result; + } + + internal EcmaMethod GetMethod(MethodDefinitionHandle handle, EcmaType owningType) + { + if (!_resolvedTokens.TryGetValue(handle, out IEntityHandleObject result)) + { + Debug.Assert(_metadataReader.GetMethodDefinition(handle).GetDeclaringType() == owningType.Handle); + result = _resolvedTokens.AddOrGetExisting(new EcmaMethod(owningType, handle)); + } + return (EcmaMethod)result; + } + public object GetObject(EntityHandle handle, NotFoundBehavior notFoundBehavior = NotFoundBehavior.Throw) { IEntityHandleObject obj = _resolvedTokens.GetOrCreateValue(handle); diff --git a/src/coreclr/tools/Common/TypeSystem/Ecma/EcmaType.cs b/src/coreclr/tools/Common/TypeSystem/Ecma/EcmaType.cs index 9e7eaa0325f94..c040ada5fbd34 100644 --- a/src/coreclr/tools/Common/TypeSystem/Ecma/EcmaType.cs +++ b/src/coreclr/tools/Common/TypeSystem/Ecma/EcmaType.cs @@ -305,7 +305,7 @@ public override IEnumerable GetMethods() { foreach (var handle in _typeDefinition.GetMethods()) { - yield return (EcmaMethod)_module.GetObject(handle); + yield return _module.GetMethod(handle, this); } } @@ -316,7 +316,7 @@ public override IEnumerable GetVirtualMethods() { MethodDefinition methodDef = reader.GetMethodDefinition(handle); if ((methodDef.Attributes & MethodAttributes.Virtual) != 0) - yield return (EcmaMethod)_module.GetObject(handle); + yield return _module.GetMethod(handle, this); } } @@ -329,7 +329,7 @@ public override MethodDesc GetMethod(string name, MethodSignature signature, Ins { if (stringComparer.Equals(metadataReader.GetMethodDefinition(handle).Name, name)) { - var method = (EcmaMethod)_module.GetObject(handle); + var method = _module.GetMethod(handle, this); if (signature == null || signature.Equals(method.Signature.ApplySubstitution(substitution))) return method; } @@ -349,7 +349,7 @@ public override MethodDesc GetStaticConstructor() if (methodDefinition.Attributes.IsRuntimeSpecialName() && stringComparer.Equals(methodDefinition.Name, ".cctor")) { - var method = (EcmaMethod)_module.GetObject(handle); + var method = _module.GetMethod(handle, this); return method; } } @@ -372,7 +372,7 @@ public override MethodDesc GetDefaultConstructor() if (attributes.IsRuntimeSpecialName() && attributes.IsPublic() && stringComparer.Equals(methodDefinition.Name, ".ctor")) { - var method = (EcmaMethod)_module.GetObject(handle); + var method = _module.GetMethod(handle, this); MethodSignature sig = method.Signature; if (sig.Length != 0) @@ -424,7 +424,7 @@ public override IEnumerable GetFields() { foreach (var handle in _typeDefinition.GetFields()) { - var field = (EcmaField)_module.GetObject(handle); + var field = _module.GetField(handle, this); yield return field; } } @@ -438,7 +438,7 @@ public override TypeDesc UnderlyingType foreach (var handle in _typeDefinition.GetFields()) { - var field = (EcmaField)_module.GetObject(handle); + var field = _module.GetField(handle, this); if (!field.IsStatic) return field.FieldType; } @@ -456,7 +456,7 @@ public override FieldDesc GetField(string name) { if (stringComparer.Equals(metadataReader.GetFieldDefinition(handle).Name, name)) { - var field = (EcmaField)_module.GetObject(handle); + var field = _module.GetField(handle, this); return field; } } @@ -560,7 +560,7 @@ public override ClassLayoutMetadata GetClassLayout() // Note: GetOffset() returns -1 when offset was not set in the metadata int specifiedOffset = fieldDefinition.GetOffset(); result.Offsets[index] = - new FieldAndOffset((EcmaField)_module.GetObject(handle), specifiedOffset == -1 ? FieldAndOffset.InvalidOffset : new LayoutInt(specifiedOffset)); + new FieldAndOffset(_module.GetField(handle, this), specifiedOffset == -1 ? FieldAndOffset.InvalidOffset : new LayoutInt(specifiedOffset)); index++; }