< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 77
Coverable lines: 77
Total lines: 174
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
DecodeSignature(...)100%110%
GetDeclaringType()100%110%
GetDefaultValue()100%110%
GetRelativeVirtualAddress()0%220%
GetOffset()0%440%
GetMarshallingDescriptor()0%220%
GetCustomAttributes()100%110%
GetProjectedName()100%110%
GetProjectedFlags()0%220%
GetProjectedSignature()100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/TypeSystem/FieldDefinition.cs

#LineLine coverage
 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
 4using System.Reflection.Metadata.Ecma335;
 5
 6namespace System.Reflection.Metadata
 7{
 8    public readonly struct FieldDefinition
 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 FieldDefinition(MetadataReader reader, uint treatmentAndRowId)
 016        {
 017            _reader = reader;
 018            _treatmentAndRowId = treatmentAndRowId;
 019        }
 20
 21        private int RowId
 22        {
 023            get { return (int)(_treatmentAndRowId & TokenTypeIds.RIDMask); }
 24        }
 25
 26        private FieldDefTreatment Treatment
 27        {
 028            get { return (FieldDefTreatment)(_treatmentAndRowId >> TokenTypeIds.RowIdBitCount); }
 29        }
 30
 31        private FieldDefinitionHandle Handle
 32        {
 033            get { return FieldDefinitionHandle.FromRowId(RowId); }
 34        }
 35
 36        public StringHandle Name
 37        {
 38            get
 039            {
 040                if (Treatment == 0)
 041                {
 042                    return _reader.FieldTable.GetName(Handle);
 43                }
 44
 045                return GetProjectedName();
 046            }
 47        }
 48
 49        public FieldAttributes Attributes
 50        {
 51            get
 052            {
 053                if (Treatment == 0)
 054                {
 055                    return _reader.FieldTable.GetFlags(Handle);
 56                }
 57
 058                return GetProjectedFlags();
 059            }
 60        }
 61
 62        public BlobHandle Signature
 63        {
 64            get
 065            {
 066                if (Treatment == 0)
 067                {
 068                    return _reader.FieldTable.GetSignature(Handle);
 69                }
 70
 071                return GetProjectedSignature();
 072            }
 73        }
 74
 75        public TType DecodeSignature<TType, TGenericContext>(ISignatureTypeProvider<TType, TGenericContext> provider, TG
 076        {
 077            var decoder = new SignatureDecoder<TType, TGenericContext>(provider, _reader, genericContext);
 078            var blob = _reader.GetBlobReader(Signature);
 079            return decoder.DecodeFieldSignature(ref blob);
 080        }
 81
 82        /// <summary>
 83        /// Returns a handle to the type that declares this field.
 84        /// </summary>
 85        public TypeDefinitionHandle GetDeclaringType()
 086        {
 087            return _reader.GetDeclaringType(Handle);
 088        }
 89
 90        public ConstantHandle GetDefaultValue()
 091        {
 092            return _reader.ConstantTable.FindConstant(Handle);
 093        }
 94
 95        public int GetRelativeVirtualAddress()
 096        {
 097            int fieldRvaRowId = _reader.FieldRvaTable.FindFieldRvaRowId(Handle.RowId);
 098            if (fieldRvaRowId == 0)
 099            {
 0100                return 0;
 101            }
 102
 0103            return _reader.FieldRvaTable.GetRva(fieldRvaRowId);
 0104        }
 105
 106        /// <summary>
 107        /// Returns field layout offset, or -1 if not available.
 108        /// </summary>
 109        public int GetOffset()
 0110        {
 0111            int layoutRowId = _reader.FieldLayoutTable.FindFieldLayoutRowId(Handle);
 0112            if (layoutRowId == 0)
 0113            {
 0114                return -1;
 115            }
 116
 0117            uint offset = _reader.FieldLayoutTable.GetOffset(layoutRowId);
 0118            if (offset > int.MaxValue)
 0119            {
 120                // CLI spec says:
 121                // "Offset (a 4-byte constant)"
 122                // "Offset shall be zero or more"
 123                //
 124                // Peverify fails with "Type load failed" error if offset is greater than Int32.MaxValue.
 0125                return -1;
 126            }
 127
 0128            return (int)offset;
 0129        }
 130
 131        public BlobHandle GetMarshallingDescriptor()
 0132        {
 0133            int marshalRowId = _reader.FieldMarshalTable.FindFieldMarshalRowId(Handle);
 0134            if (marshalRowId == 0)
 0135            {
 0136                return default(BlobHandle);
 137            }
 138
 0139            return _reader.FieldMarshalTable.GetNativeType(marshalRowId);
 0140        }
 141
 142        public CustomAttributeHandleCollection GetCustomAttributes()
 0143        {
 0144            return new CustomAttributeHandleCollection(_reader, Handle);
 0145        }
 146
 147#region Projections
 148
 149        private StringHandle GetProjectedName()
 0150        {
 151            // no change:
 0152            return _reader.FieldTable.GetName(Handle);
 0153        }
 154
 155        private FieldAttributes GetProjectedFlags()
 0156        {
 0157            var flags = _reader.FieldTable.GetFlags(Handle);
 158
 0159            if (Treatment == FieldDefTreatment.EnumValue)
 0160            {
 0161                return (flags & ~FieldAttributes.FieldAccessMask) | FieldAttributes.Public;
 162            }
 163
 0164            return flags;
 0165        }
 166
 167        private BlobHandle GetProjectedSignature()
 0168        {
 0169            return _reader.FieldTable.GetSignature(Handle);
 0170        }
 171#endregion
 172    }
 173}
 174