| | | 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 | | |
| | | 4 | | using System.Reflection.Metadata.Ecma335; |
| | | 5 | | |
| | | 6 | | namespace System.Reflection.Metadata |
| | | 7 | | { |
| | | 8 | | public readonly struct MemberReference |
| | | 9 | | { |
| | | 10 | | private readonly MetadataReader _reader; |
| | | 11 | | |
| | | 12 | | // Workaround: JIT doesn't generate good code for nested structures, so use RowId. |
| | | 13 | | private readonly uint _treatmentAndRowId; |
| | | 14 | | |
| | | 15 | | internal MemberReference(MetadataReader reader, uint treatmentAndRowId) |
| | 0 | 16 | | { |
| | 0 | 17 | | _reader = reader; |
| | 0 | 18 | | _treatmentAndRowId = treatmentAndRowId; |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | private int RowId |
| | | 22 | | { |
| | 0 | 23 | | get { return (int)(_treatmentAndRowId & TokenTypeIds.RIDMask); } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | private MemberRefTreatment Treatment |
| | | 27 | | { |
| | 0 | 28 | | get { return (MemberRefTreatment)(_treatmentAndRowId >> TokenTypeIds.RowIdBitCount); } |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | private MemberReferenceHandle Handle |
| | | 32 | | { |
| | 0 | 33 | | get { return MemberReferenceHandle.FromRowId(RowId); } |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// MethodDef, ModuleRef,TypeDef, TypeRef, or TypeSpec handle. |
| | | 38 | | /// </summary> |
| | | 39 | | public EntityHandle Parent |
| | | 40 | | { |
| | | 41 | | get |
| | 0 | 42 | | { |
| | 0 | 43 | | if (Treatment == 0) |
| | 0 | 44 | | { |
| | 0 | 45 | | return _reader.MemberRefTable.GetClass(Handle); |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | return GetProjectedParent(); |
| | 0 | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public StringHandle Name |
| | | 53 | | { |
| | | 54 | | get |
| | 0 | 55 | | { |
| | 0 | 56 | | if (Treatment == 0) |
| | 0 | 57 | | { |
| | 0 | 58 | | return _reader.MemberRefTable.GetName(Handle); |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | return GetProjectedName(); |
| | 0 | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets a handle to the signature blob. |
| | | 67 | | /// </summary> |
| | | 68 | | public BlobHandle Signature |
| | | 69 | | { |
| | | 70 | | get |
| | 0 | 71 | | { |
| | 0 | 72 | | if (Treatment == 0) |
| | 0 | 73 | | { |
| | 0 | 74 | | return _reader.MemberRefTable.GetSignature(Handle); |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | return GetProjectedSignature(); |
| | 0 | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public TType DecodeFieldSignature<TType, TGenericContext>(ISignatureTypeProvider<TType, TGenericContext> provide |
| | 0 | 82 | | { |
| | 0 | 83 | | var decoder = new SignatureDecoder<TType, TGenericContext>(provider, _reader, genericContext); |
| | 0 | 84 | | var blobReader = _reader.GetBlobReader(Signature); |
| | 0 | 85 | | return decoder.DecodeFieldSignature(ref blobReader); |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | public MethodSignature<TType> DecodeMethodSignature<TType, TGenericContext>(ISignatureTypeProvider<TType, TGener |
| | 0 | 89 | | { |
| | 0 | 90 | | var decoder = new SignatureDecoder<TType, TGenericContext>(provider, _reader, genericContext); |
| | 0 | 91 | | var blobReader = _reader.GetBlobReader(Signature); |
| | 0 | 92 | | return decoder.DecodeMethodSignature(ref blobReader); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Determines if the member reference is to a method or field. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <exception cref="BadImageFormatException">The member reference signature is invalid.</exception> |
| | | 99 | | public MemberReferenceKind GetKind() |
| | 0 | 100 | | { |
| | 0 | 101 | | BlobReader blobReader = _reader.GetBlobReader(this.Signature); |
| | 0 | 102 | | SignatureHeader header = blobReader.ReadSignatureHeader(); |
| | | 103 | | |
| | 0 | 104 | | return header.Kind switch |
| | 0 | 105 | | { |
| | 0 | 106 | | SignatureKind.Method => MemberReferenceKind.Method, |
| | 0 | 107 | | SignatureKind.Field => MemberReferenceKind.Field, |
| | 0 | 108 | | _ => throw new BadImageFormatException(), |
| | 0 | 109 | | }; |
| | 0 | 110 | | } |
| | | 111 | | |
| | | 112 | | public CustomAttributeHandleCollection GetCustomAttributes() |
| | 0 | 113 | | { |
| | 0 | 114 | | return new CustomAttributeHandleCollection(_reader, Handle); |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | #region Projections |
| | | 118 | | |
| | | 119 | | private EntityHandle GetProjectedParent() |
| | 0 | 120 | | { |
| | | 121 | | // no change |
| | 0 | 122 | | return _reader.MemberRefTable.GetClass(Handle); |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | private StringHandle GetProjectedName() |
| | 0 | 126 | | { |
| | 0 | 127 | | if (Treatment == MemberRefTreatment.Dispose) |
| | 0 | 128 | | { |
| | 0 | 129 | | return StringHandle.FromVirtualIndex(StringHandle.VirtualIndex.Dispose); |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | return _reader.MemberRefTable.GetName(Handle); |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | private BlobHandle GetProjectedSignature() |
| | 0 | 136 | | { |
| | | 137 | | // no change |
| | 0 | 138 | | return _reader.MemberRefTable.GetSignature(Handle); |
| | 0 | 139 | | } |
| | | 140 | | #endregion |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | |