예제 #1
0
        private int GetConstructionOrder(MetadataType type)
        {
            // For EagerOrderedStaticConstructorAttribute, order is defined by an integer.
            // For the other case (EagerStaticClassConstructionAttribute), order is defined
            // implicitly.

            type = (MetadataType)type.GetTypeDefinition();

            EcmaType       ecmaType       = (EcmaType)type;
            MetadataReader metadataReader = ecmaType.MetadataReader;

            foreach (var attributeHandle in metadataReader.GetTypeDefinition(ecmaType.Handle).GetCustomAttributes())
            {
                EntityHandle attributeType, attributeCtor;
                if (!metadataReader.GetAttributeTypeAndConstructor(attributeHandle,
                                                                   out attributeType, out attributeCtor))
                {
                    continue;
                }

                StringHandle namespaceHandle, nameHandle;
                if (!metadataReader.GetAttributeTypeNamespaceAndName(attributeType,
                                                                     out namespaceHandle, out nameHandle))
                {
                    continue;
                }

                if (metadataReader.StringComparer.Equals(namespaceHandle, "System.Runtime.CompilerServices") &&
                    metadataReader.StringComparer.Equals(nameHandle, "EagerOrderedStaticConstructorAttribute"))
                {
                    var attributeBlob = metadataReader.GetCustomAttributeBlobReader(attributeHandle);
                    return(attributeBlob.ReadInt32());
                }
            }

            Debug.Assert(type.HasCustomAttribute("System.Runtime.CompilerServices", "EagerStaticClassConstructionAttribute"));
            // RhBind on .NET Native for UWP will sort these based on static dependencies of the .cctors.
            // We could probably do the same, but this attribute is pretty much deprecated in favor of
            // EagerOrderedStaticConstructorAttribute that has explicit order. The remaining uses of
            // the unordered one don't appear to have dependencies, so sorting them all before the
            // ordered ones should do.
            return(-1);
        }