Beispiel #1
0
        public static CustomAttributeValue <TypeDesc>?GetDecodedCustomAttribute(this EcmaField This,
                                                                                string attributeNamespace, string attributeName)
        {
            var metadataReader = This.MetadataReader;

            var attributeHandle = metadataReader.GetCustomAttributeHandle(metadataReader.GetFieldDefinition(This.Handle).GetCustomAttributes(),
                                                                          attributeNamespace, attributeName);

            if (attributeHandle.IsNil)
            {
                return(null);
            }

            return(metadataReader.GetCustomAttribute(attributeHandle).DecodeValue(new CustomAttributeTypeProvider(This.Module)));
        }
Beispiel #2
0
        /// <summary>
        /// Retrieves the data associated with an RVA mapped field from the PE module.
        /// </summary>
        public static byte[] GetFieldRvaData(this EcmaField field)
        {
            Debug.Assert(field.HasRva);
            int addr     = field.MetadataReader.GetFieldDefinition(field.Handle).GetRelativeVirtualAddress();
            var memBlock = field.Module.PEReader.GetSectionData(addr).GetContent();

            int size = field.FieldType.GetElementSize();

            if (size > memBlock.Length)
            {
                throw new BadImageFormatException();
            }

            byte[] result = new byte[size];
            memBlock.CopyTo(0, result, 0, result.Length);

            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Retrieves the data associated with an RVA mapped field from the PE module.
        /// </summary>
        public static byte[] GetFieldRvaData(this EcmaField field)
        {
            Debug.Assert(field.HasRva);
            int addr     = field.MetadataReader.GetFieldDefinition(field.Handle).GetRelativeVirtualAddress();
            var memBlock = field.Module.PEReader.GetSectionData(addr).GetContent();

            var fieldType = (EcmaType)field.FieldType;
            int size      = fieldType.MetadataReader.GetTypeDefinition(fieldType.Handle).GetLayout().Size;

            if (size == 0)
            {
                throw new NotImplementedException();
            }

            byte[] result = new byte[size];
            memBlock.CopyTo(0, result, 0, result.Length);

            return(result);
        }
Beispiel #4
0
        public static IEnumerable <CustomAttributeValue <TypeDesc> > GetDecodedCustomAttributes(this EcmaField This,
                                                                                                string attributeNamespace, string attributeName)
        {
            var metadataReader   = This.MetadataReader;
            var attributeHandles = metadataReader.GetFieldDefinition(This.Handle).GetCustomAttributes();

            foreach (var attributeHandle in attributeHandles)
            {
                if (IsEqualCustomAttributeName(attributeHandle, metadataReader, attributeNamespace, attributeName))
                {
                    yield return(metadataReader.GetCustomAttribute(attributeHandle).DecodeValue(new CustomAttributeTypeProvider(This.Module)));
                }
            }
        }
Beispiel #5
0
            protected override IEntityHandleObject CreateValueFromKey(EntityHandle handle)
            {
                object item;

                switch (handle.Kind)
                {
                case HandleKind.TypeDefinition:
                    item = new EcmaType(_module, (TypeDefinitionHandle)handle);
                    break;

                case HandleKind.MethodDefinition:
                {
                    MethodDefinitionHandle methodDefinitionHandle = (MethodDefinitionHandle)handle;
                    TypeDefinitionHandle   typeDefinitionHandle   = _module._metadataReader.GetMethodDefinition(methodDefinitionHandle).GetDeclaringType();
                    EcmaType type = (EcmaType)_module.GetObject(typeDefinitionHandle);
                    item = new EcmaMethod(type, methodDefinitionHandle);
                }
                break;

                case HandleKind.FieldDefinition:
                {
                    FieldDefinitionHandle fieldDefinitionHandle = (FieldDefinitionHandle)handle;
                    TypeDefinitionHandle  typeDefinitionHandle  = _module._metadataReader.GetFieldDefinition(fieldDefinitionHandle).GetDeclaringType();
                    EcmaType type = (EcmaType)_module.GetObject(typeDefinitionHandle);
                    item = new EcmaField(type, fieldDefinitionHandle);
                }
                break;

                case HandleKind.TypeReference:
                    item = _module.ResolveTypeReference((TypeReferenceHandle)handle);
                    break;

                case HandleKind.MemberReference:
                    item = _module.ResolveMemberReference((MemberReferenceHandle)handle);
                    break;

                case HandleKind.AssemblyReference:
                    item = _module.ResolveAssemblyReference((AssemblyReferenceHandle)handle);
                    break;

                case HandleKind.TypeSpecification:
                    item = _module.ResolveTypeSpecification((TypeSpecificationHandle)handle);
                    break;

                case HandleKind.MethodSpecification:
                    item = _module.ResolveMethodSpecification((MethodSpecificationHandle)handle);
                    break;

                case HandleKind.ExportedType:
                    item = _module.ResolveExportedType((ExportedTypeHandle)handle);
                    break;

                case HandleKind.StandaloneSignature:
                    item = _module.ResolveStandaloneSignature((StandaloneSignatureHandle)handle);
                    break;

                case HandleKind.ModuleDefinition:
                    // ECMA-335 Partition 2 II.22.38 1d: This should not occur in a CLI ("compressed metadata") module,
                    // but resolves to "current module".
                    item = _module;
                    break;

                default:
                    throw new BadImageFormatException("Unknown metadata token type: " + handle.Kind);
                }

                switch (handle.Kind)
                {
                case HandleKind.TypeDefinition:
                case HandleKind.MethodDefinition:
                case HandleKind.FieldDefinition:
                    // type/method/field definitions directly correspond to their target item.
                    return((IEntityHandleObject)item);

                default:
                    // Everything else is some form of reference which cannot be self-describing
                    return(new EcmaObjectLookupWrapper(handle, item));
                }
            }
Beispiel #6
0
 /// <summary>
 /// Returns the RVA associated with an RVA mapped field from the PE module.
 /// </summary>
 public static int GetFieldRvaValue(this EcmaField field)
 {
     Debug.Assert(field.HasRva);
     return(field.MetadataReader.GetFieldDefinition(field.Handle).GetRelativeVirtualAddress());
 }
        public static IEnumerable <CustomAttributeValue <TypeDesc> > GetDecodedCustomAttributes(this EcmaField This,
                                                                                                string attributeNamespace, string attributeName)
        {
            var metadataReader   = This.MetadataReader;
            var attributeHandles = metadataReader.GetFieldDefinition(This.Handle).GetCustomAttributes();

            foreach (var attributeHandle in attributeHandles)
            {
                StringHandle namespaceHandle, nameHandle;
                if (!metadataReader.GetAttributeNamespaceAndName(attributeHandle, out namespaceHandle, out nameHandle))
                {
                    continue;
                }

                if (metadataReader.StringComparer.Equals(namespaceHandle, attributeNamespace) &&
                    metadataReader.StringComparer.Equals(nameHandle, attributeName))
                {
                    yield return(metadataReader.GetCustomAttribute(attributeHandle).DecodeValue(new CustomAttributeTypeProvider(This.Module)));
                }
            }
        }
Beispiel #8
0
            protected override IEntityHandleObject CreateValueFromKey(EntityHandle handle)
            {
                object item;

                switch (handle.Kind)
                {
                case HandleKind.TypeDefinition:
                    item = new EcmaType(_module, (TypeDefinitionHandle)handle);
                    break;

                case HandleKind.MethodDefinition:
                {
                    MethodDefinitionHandle methodDefinitionHandle = (MethodDefinitionHandle)handle;
                    TypeDefinitionHandle   typeDefinitionHandle   = _module._metadataReader.GetMethodDefinition(methodDefinitionHandle).GetDeclaringType();
                    EcmaType type = (EcmaType)_module.GetObject(typeDefinitionHandle);
                    item = new EcmaMethod(type, methodDefinitionHandle);
                }
                break;

                case HandleKind.FieldDefinition:
                {
                    FieldDefinitionHandle fieldDefinitionHandle = (FieldDefinitionHandle)handle;
                    TypeDefinitionHandle  typeDefinitionHandle  = _module._metadataReader.GetFieldDefinition(fieldDefinitionHandle).GetDeclaringType();
                    EcmaType type = (EcmaType)_module.GetObject(typeDefinitionHandle);
                    item = new EcmaField(type, fieldDefinitionHandle);
                }
                break;

                case HandleKind.TypeReference:
                    item = _module.ResolveTypeReference((TypeReferenceHandle)handle);
                    break;

                case HandleKind.MemberReference:
                    item = _module.ResolveMemberReference((MemberReferenceHandle)handle);
                    break;

                case HandleKind.AssemblyReference:
                    item = _module.ResolveAssemblyReference((AssemblyReferenceHandle)handle);
                    break;

                case HandleKind.TypeSpecification:
                    item = _module.ResolveTypeSpecification((TypeSpecificationHandle)handle);
                    break;

                case HandleKind.MethodSpecification:
                    item = _module.ResolveMethodSpecification((MethodSpecificationHandle)handle);
                    break;

                case HandleKind.ExportedType:
                    item = _module.ResolveExportedType((ExportedTypeHandle)handle);
                    break;

                // TODO: Resolve other tokens

                default:
                    throw new BadImageFormatException("Unknown metadata token type");
                }

                switch (handle.Kind)
                {
                case HandleKind.TypeDefinition:
                case HandleKind.MethodDefinition:
                case HandleKind.FieldDefinition:
                    // type/method/field definitions directly correspond to their target item.
                    return((IEntityHandleObject)item);

                default:
                    // Everything else is some form of reference which cannot be self-describing
                    return(new EcmaObjectLookupWrapper(handle, item));
                }
            }