| | | 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 MethodDefinition |
| | | 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 MethodDefinition(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 MethodDefTreatment Treatment |
| | | 27 | | { |
| | 0 | 28 | | get { return (MethodDefTreatment)(_treatmentAndRowId >> TokenTypeIds.RowIdBitCount); } |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | private MethodDefinitionHandle Handle |
| | | 32 | | { |
| | 0 | 33 | | get { return MethodDefinitionHandle.FromRowId(RowId); } |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public StringHandle Name |
| | | 37 | | { |
| | | 38 | | get |
| | 0 | 39 | | { |
| | 0 | 40 | | if (Treatment == 0) |
| | 0 | 41 | | { |
| | 0 | 42 | | return _reader.MethodDefTable.GetName(Handle); |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | return GetProjectedName(); |
| | 0 | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public BlobHandle Signature |
| | | 50 | | { |
| | | 51 | | get |
| | 0 | 52 | | { |
| | 0 | 53 | | if (Treatment == 0) |
| | 0 | 54 | | { |
| | 0 | 55 | | return _reader.MethodDefTable.GetSignature(Handle); |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | return GetProjectedSignature(); |
| | 0 | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public MethodSignature<TType> DecodeSignature<TType, TGenericContext>(ISignatureTypeProvider<TType, TGenericCont |
| | 0 | 63 | | { |
| | 0 | 64 | | var decoder = new SignatureDecoder<TType, TGenericContext>(provider, _reader, genericContext); |
| | 0 | 65 | | var blobReader = _reader.GetBlobReader(Signature); |
| | 0 | 66 | | return decoder.DecodeMethodSignature(ref blobReader); |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | public int RelativeVirtualAddress |
| | | 70 | | { |
| | | 71 | | get |
| | 0 | 72 | | { |
| | 0 | 73 | | if (Treatment == 0) |
| | 0 | 74 | | { |
| | 0 | 75 | | return _reader.MethodDefTable.GetRva(Handle); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | return GetProjectedRelativeVirtualAddress(); |
| | 0 | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | public MethodAttributes Attributes |
| | | 83 | | { |
| | | 84 | | get |
| | 0 | 85 | | { |
| | 0 | 86 | | if (Treatment == 0) |
| | 0 | 87 | | { |
| | 0 | 88 | | return _reader.MethodDefTable.GetFlags(Handle); |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | return GetProjectedFlags(); |
| | 0 | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | public MethodImplAttributes ImplAttributes |
| | | 96 | | { |
| | | 97 | | get |
| | 0 | 98 | | { |
| | 0 | 99 | | if (Treatment == 0) |
| | 0 | 100 | | { |
| | 0 | 101 | | return _reader.MethodDefTable.GetImplFlags(Handle); |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | return GetProjectedImplFlags(); |
| | 0 | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Returns a handle to the type that declares this method. |
| | | 110 | | /// </summary> |
| | | 111 | | public TypeDefinitionHandle GetDeclaringType() |
| | 0 | 112 | | { |
| | 0 | 113 | | return _reader.GetDeclaringType(Handle); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | public ParameterHandleCollection GetParameters() |
| | 0 | 117 | | { |
| | 0 | 118 | | return new ParameterHandleCollection(_reader, Handle); |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | public GenericParameterHandleCollection GetGenericParameters() |
| | 0 | 122 | | { |
| | 0 | 123 | | return _reader.GenericParamTable.FindGenericParametersForMethod(Handle); |
| | 0 | 124 | | } |
| | | 125 | | |
| | | 126 | | public MethodImport GetImport() |
| | 0 | 127 | | { |
| | 0 | 128 | | int implMapRid = _reader.ImplMapTable.FindImplForMethod(Handle); |
| | 0 | 129 | | if (implMapRid == 0) |
| | 0 | 130 | | { |
| | 0 | 131 | | return default(MethodImport); |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | return _reader.ImplMapTable.GetImport(implMapRid); |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | public CustomAttributeHandleCollection GetCustomAttributes() |
| | 0 | 138 | | { |
| | 0 | 139 | | return new CustomAttributeHandleCollection(_reader, Handle); |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | public DeclarativeSecurityAttributeHandleCollection GetDeclarativeSecurityAttributes() |
| | 0 | 143 | | { |
| | 0 | 144 | | return new DeclarativeSecurityAttributeHandleCollection(_reader, Handle); |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | #region Projections |
| | | 148 | | |
| | | 149 | | private StringHandle GetProjectedName() |
| | 0 | 150 | | { |
| | 0 | 151 | | if ((Treatment & MethodDefTreatment.KindMask) == MethodDefTreatment.DisposeMethod) |
| | 0 | 152 | | { |
| | 0 | 153 | | return StringHandle.FromVirtualIndex(StringHandle.VirtualIndex.Dispose); |
| | | 154 | | } |
| | | 155 | | |
| | 0 | 156 | | return _reader.MethodDefTable.GetName(Handle); |
| | 0 | 157 | | } |
| | | 158 | | |
| | | 159 | | private MethodAttributes GetProjectedFlags() |
| | 0 | 160 | | { |
| | 0 | 161 | | MethodAttributes flags = _reader.MethodDefTable.GetFlags(Handle); |
| | 0 | 162 | | MethodDefTreatment treatment = Treatment; |
| | | 163 | | |
| | 0 | 164 | | if ((treatment & MethodDefTreatment.KindMask) == MethodDefTreatment.HiddenInterfaceImplementation) |
| | 0 | 165 | | { |
| | 0 | 166 | | flags = (flags & ~MethodAttributes.MemberAccessMask) | MethodAttributes.Private; |
| | 0 | 167 | | } |
| | | 168 | | |
| | 0 | 169 | | if ((treatment & MethodDefTreatment.MarkAbstractFlag) != 0) |
| | 0 | 170 | | { |
| | 0 | 171 | | flags |= MethodAttributes.Abstract; |
| | 0 | 172 | | } |
| | | 173 | | |
| | 0 | 174 | | if ((treatment & MethodDefTreatment.MarkPublicFlag) != 0) |
| | 0 | 175 | | { |
| | 0 | 176 | | flags = (flags & ~MethodAttributes.MemberAccessMask) | MethodAttributes.Public; |
| | 0 | 177 | | } |
| | | 178 | | |
| | | 179 | | |
| | 0 | 180 | | return flags | MethodAttributes.HideBySig; |
| | 0 | 181 | | } |
| | | 182 | | |
| | | 183 | | private MethodImplAttributes GetProjectedImplFlags() |
| | 0 | 184 | | { |
| | 0 | 185 | | MethodImplAttributes flags = _reader.MethodDefTable.GetImplFlags(Handle); |
| | | 186 | | |
| | 0 | 187 | | switch (Treatment & MethodDefTreatment.KindMask) |
| | | 188 | | { |
| | | 189 | | case MethodDefTreatment.DelegateMethod: |
| | 0 | 190 | | flags |= MethodImplAttributes.Runtime; |
| | 0 | 191 | | break; |
| | | 192 | | |
| | | 193 | | case MethodDefTreatment.DisposeMethod: |
| | | 194 | | case MethodDefTreatment.AttributeMethod: |
| | | 195 | | case MethodDefTreatment.InterfaceMethod: |
| | | 196 | | case MethodDefTreatment.HiddenInterfaceImplementation: |
| | | 197 | | case MethodDefTreatment.Other: |
| | 0 | 198 | | flags |= MethodImplAttributes.Runtime | MethodImplAttributes.InternalCall; |
| | 0 | 199 | | break; |
| | | 200 | | } |
| | | 201 | | |
| | 0 | 202 | | return flags; |
| | 0 | 203 | | } |
| | | 204 | | |
| | | 205 | | private BlobHandle GetProjectedSignature() |
| | 0 | 206 | | { |
| | 0 | 207 | | return _reader.MethodDefTable.GetSignature(Handle); |
| | 0 | 208 | | } |
| | | 209 | | |
| | | 210 | | private static int GetProjectedRelativeVirtualAddress() |
| | | 211 | | { |
| | | 212 | | return 0; |
| | | 213 | | } |
| | | 214 | | #endregion |
| | | 215 | | } |
| | | 216 | | } |
| | | 217 | | |