Esempio n. 1
0
        public static bool TryFindRawDefaultValueFromCustomAttributes(this CustomAttributeHandleCollection handles, EcmaModule module, out object?rawDefaultValue)
        {
            rawDefaultValue = default;

            MetadataReader reader = module.Reader;

            foreach (CustomAttributeHandle handle in handles)
            {
                CustomAttribute ca = handle.GetCustomAttribute(reader);
                EntityHandle    declaringTypeHandle = ca.TryGetDeclaringTypeHandle(reader);
                if (declaringTypeHandle.IsNil)
                {
                    continue;
                }

                if (declaringTypeHandle.TypeMatchesNameAndNamespace(Utf8Constants.SystemRuntimeCompilerServices, Utf8Constants.DateTimeConstantAttribute, reader))
                {
                    CustomAttributeData cad = handle.ToCustomAttributeData(module);
                    IList <CustomAttributeTypedArgument> cats = cad.ConstructorArguments;
                    if (cats.Count != 1)
                    {
                        return(false);
                    }

                    CoreTypes ct = module.Loader.GetAllFoundCoreTypes();
                    if (cats[0].ArgumentType != ct[CoreType.Int64])
                    {
                        return(false);
                    }

                    long ticks = (long)(cats[0].Value !);
                    rawDefaultValue = new DateTimeConstantAttribute(ticks).Value;
                    return(true);
                }

                if (declaringTypeHandle.TypeMatchesNameAndNamespace(Utf8Constants.SystemRuntimeCompilerServices, Utf8Constants.DecimalConstantAttribute, reader))
                {
                    CustomAttributeData cad = handle.ToCustomAttributeData(module);
                    IList <CustomAttributeTypedArgument> cats = cad.ConstructorArguments;
                    if (cats.Count != 5)
                    {
                        return(false);
                    }

                    CoreTypes ct = module.Loader.GetAllFoundCoreTypes();
                    if (cats[0].ArgumentType != ct[CoreType.Byte] ||
                        cats[1].ArgumentType != ct[CoreType.Byte])
                    {
                        return(false);
                    }

                    byte scale = (byte)cats[0].Value !;
                    byte sign  = (byte)cats[1].Value !;

                    if (cats[2].ArgumentType == ct[CoreType.Int32] && cats[3].ArgumentType == ct[CoreType.Int32] && cats[4].ArgumentType == ct[CoreType.Int32])
                    {
                        int hi  = (int)cats[2].Value !;
                        int mid = (int)cats[3].Value !;
                        int lo  = (int)cats[4].Value !;
                        rawDefaultValue = new DecimalConstantAttribute(scale, sign, hi, mid, lo).Value;
                        return(true);
                    }

                    if (cats[2].ArgumentType == ct[CoreType.UInt32] && cats[3].ArgumentType == ct[CoreType.UInt32] && cats[4].ArgumentType == ct[CoreType.UInt32])
                    {
                        uint hi  = (uint)cats[2].Value !;
                        uint mid = (uint)cats[3].Value !;
                        uint lo  = (uint)cats[4].Value !;
                        rawDefaultValue = new DecimalConstantAttribute(scale, sign, hi, mid, lo).Value;
                        return(true);
                    }

                    return(false);
                }

                // Should we also look for CustomConstantAttribute too? Who uses that (other than DateTimeConstantAttribute which
                // we handled above?) CustomConstantAttribute is an abstract class which means we have to figure out how a subclass
                // we've never heard of would set the "Value" property which is kinda hard to do when you can't Invoke().
                // Even the CLR doesn't return consistent values for this between the raw and non-raw versions.
                // Even doing the subclass check would open the door to resolving types and dependency assemblies and their
                // resulting FileNotFoundExceptions. Which is exactly what we're trying to avoid with this name-based lookup approach.
            }

            return(false);
        }
 internal EcmaModifiedTypeProvider(EcmaModule module)
     : base(module)
 {
 }
        public static CustomAttributeData TryFindCustomAttribute(this CustomAttributeHandleCollection handles, ReadOnlySpan <byte> ns, ReadOnlySpan <byte> name, EcmaModule module)
        {
            CustomAttributeHandle handle = handles.FindCustomAttributeByName(ns, name, module);

            if (handle.IsNil)
            {
                return(null);
            }
            return(handle.ToCustomAttributeData(module));
        }
        private static CustomAttributeHandle FindCustomAttributeByName(this CustomAttributeHandleCollection handles, ReadOnlySpan <byte> ns, ReadOnlySpan <byte> name, EcmaModule module)
        {
            MetadataReader reader = module.Reader;

            foreach (CustomAttributeHandle handle in handles)
            {
                CustomAttribute ca = handle.GetCustomAttribute(reader);
                EntityHandle    declaringTypeHandle = ca.TryGetDeclaringTypeHandle(reader);
                if (declaringTypeHandle.IsNil)
                {
                    continue;
                }
                if (declaringTypeHandle.TypeMatchesNameAndNamespace(ns, name, reader))
                {
                    return(handle);
                }
            }

            return(default);
 public static CustomAttributeData ToCustomAttributeData(this CustomAttributeHandle handle, EcmaModule module) => new EcmaCustomAttributeData(handle, module);
 public static bool IsCustomAttributeDefined(this CustomAttributeHandleCollection handles, ReadOnlySpan <byte> ns, ReadOnlySpan <byte> name, EcmaModule module)
 {
     return(!handles.FindCustomAttributeByName(ns, name, module).IsNil);
 }
Esempio n. 7
0
 public static RoType ResolveTypeDefRefOrSpec(this EntityHandle handle, EcmaModule module, in TypeContext typeContext)
 /// <summary>
 /// Converts ECMA-encoded custom attributes into a freshly allocated CustomAttributeData object suitable for direct return
 /// from the CustomAttributes api.
 /// </summary>
 public static IEnumerable <CustomAttributeData> ToTrueCustomAttributes(this CustomAttributeHandleCollection handles, EcmaModule module)
 {
     foreach (CustomAttributeHandle handle in handles)
     {
         yield return(handle.ToCustomAttributeData(module));
     }
 }
 internal EcmaCustomAttributeData(CustomAttributeHandle handle, EcmaModule module)
 {
     _handle = handle;
     _module = module;
     _neverAccessThisExceptThroughCustomAttributeProperty = handle.GetCustomAttribute(Reader);
 }
        internal unsafe InternalManifestResourceInfo GetInternalManifestResourceInfo(string resourceName)
        {
            MetadataReader reader = Reader;

            InternalManifestResourceInfo     result            = new InternalManifestResourceInfo();
            ManifestResourceHandleCollection manifestResources = reader.ManifestResources;

            foreach (ManifestResourceHandle resourceHandle in manifestResources)
            {
                ManifestResource resource = resourceHandle.GetManifestResource(reader);
                if (resource.Name.Equals(resourceName, reader))
                {
                    result.Found = true;
                    if (resource.Implementation.IsNil)
                    {
                        checked
                        {
                            // Embedded data resource
                            result.ResourceLocation = ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile;
                            PEReader pe = _guardedPEReader.PEReader;

                            PEMemoryBlock resourceDirectory = pe.GetSectionData(pe.PEHeaders.CorHeader.ResourcesDirectory.RelativeVirtualAddress);
                            BlobReader    blobReader        = resourceDirectory.GetReader((int)resource.Offset, resourceDirectory.Length - (int)resource.Offset);
                            uint          length            = blobReader.ReadUInt32();
                            result.PointerToResource = blobReader.CurrentPointer;

                            // Length check the size of the resource to ensure it fits in the PE file section, note, this is only safe as its in a checked region
                            if (length + sizeof(Int32) > blobReader.Length)
                            {
                                throw new BadImageFormatException();
                            }
                            result.SizeOfResource = length;
                        }
                    }
                    else
                    {
                        if (resource.Implementation.Kind == HandleKind.AssemblyFile)
                        {
                            // Get file name
                            result.ResourceLocation = default;
                            AssemblyFile file = ((AssemblyFileHandle)resource.Implementation).GetAssemblyFile(reader);
                            result.FileName = file.Name.GetString(reader);
                            if (file.ContainsMetadata)
                            {
                                EcmaModule module = (EcmaModule)Assembly.GetModule(result.FileName);
                                if (module == null)
                                {
                                    throw new BadImageFormatException(SR.Format(SR.ManifestResourceInfoReferencedBadModule, result.FileName));
                                }
                                result = module.GetInternalManifestResourceInfo(resourceName);
                            }
                        }
                        else if (resource.Implementation.Kind == HandleKind.AssemblyReference)
                        {
                            // Resolve assembly reference
                            result.ResourceLocation = ResourceLocation.ContainedInAnotherAssembly;
                            RoAssemblyName destinationAssemblyName = ((AssemblyReferenceHandle)resource.Implementation).ToRoAssemblyName(reader);
                            result.ReferencedAssembly = Loader.ResolveAssembly(destinationAssemblyName);
                        }
                    }
                }
            }

            return(result);
        }
Esempio n. 11
0
 protected EcmaWrappedTypeProvider(EcmaModule module)
 {
     _module       = module;
     _typeProvider = module;
 }
Esempio n. 12
0
 internal EcmaGenericTypeParameterType(GenericParameterHandle handle, EcmaModule module)
     : base(handle, module)
 {
 }
 internal EcmaGenericMethodParameterType(GenericParameterHandle handle, EcmaModule module)
     : base(handle, module)
 {
     Debug.Assert(!handle.IsNil);
 }