예제 #1
0
        protected sealed override Guid?ComputeGuidFromCustomAttributes()
        {
            //
            // Look for a [Guid] attribute. If found, return that.
            //
            foreach (CustomAttributeHandle cah in _typeDefinition.GetCustomAttributes())
            {
                // We can't reference the GuidAttribute class directly as we don't have an official dependency on System.Runtime.InteropServices.
                // Following age-old CLR tradition, we search for the custom attribute using a name-based search. Since this makes it harder
                // to be sure we won't run into custom attribute constructors that comply with the GuidAttribute(String) signature,
                // we'll check that it does and silently skip the CA if it doesn't match the expected pattern.
                CustomAttribute attribute = Reader.GetCustomAttribute(cah);
                EntityHandle    ctorType;
                EcmaMetadataHelpers.GetAttributeTypeDefRefOrSpecHandle(_reader, attribute.Constructor, out ctorType);
                StringHandle typeNameHandle;
                StringHandle typeNamespaceHandle;
                if (EcmaMetadataHelpers.GetAttributeNamespaceAndName(Reader, ctorType, out typeNamespaceHandle, out typeNameHandle))
                {
                    MetadataStringComparer stringComparer = Reader.StringComparer;
                    if (stringComparer.Equals(typeNamespaceHandle, "System.Runtime.InteropServices"))
                    {
                        if (stringComparer.Equals(typeNameHandle, "GuidAttribute"))
                        {
                            ReflectionTypeProvider typeProvider = new ReflectionTypeProvider(throwOnError: false);

                            CustomAttributeValue <RuntimeTypeInfo> customAttributeValue = attribute.DecodeValue(typeProvider);
                            if (customAttributeValue.FixedArguments.Length != 1)
                            {
                                continue;
                            }

                            CustomAttributeTypedArgument <RuntimeTypeInfo> firstArg = customAttributeValue.FixedArguments[0];
                            if (firstArg.Value == null)
                            {
                                continue;
                            }

                            string guidString = firstArg.Value as string;
                            if (guidString == null)
                            {
                                continue;
                            }

                            return(new Guid(guidString));
                        }
                    }
                }
            }

            return(null);
        }
        internal sealed override IEnumerable <MethodInfo> CoreGetDeclaredMethods(NameFilter optionalNameFilter, RuntimeTypeInfo reflectedType, RuntimeTypeInfo contextTypeInfo)
        {
            MetadataReader reader = Reader;

            foreach (MethodDefinitionHandle methodHandle in DeclaredMethodAndConstructorHandles)
            {
                MethodDefinition method = reader.GetMethodDefinition(methodHandle);

                if (EcmaMetadataHelpers.IsConstructor(ref method, reader))
                {
                    continue;
                }

                if (optionalNameFilter == null || optionalNameFilter.Matches(method.Name, reader))
                {
                    yield return(RuntimeNamedMethodInfo <EcmaFormatMethodCommon> .GetRuntimeNamedMethodInfo(new EcmaFormatMethodCommon(methodHandle, this, contextTypeInfo), reflectedType));
                }
            }
        }
        public static bool IsCustomAttributeOfType(this CustomAttributeHandle handle, MetadataReader reader, string ns, string name)
        {
            CustomAttribute attribute = reader.GetCustomAttribute(handle);

            EcmaMetadataHelpers.GetAttributeTypeDefRefOrSpecHandle(reader, attribute.Constructor, out EntityHandle typeDefOrRefOrSpec);

            switch (typeDefOrRefOrSpec.Kind)
            {
            case HandleKind.TypeReference:
                TypeReference typeRef    = reader.GetTypeReference((TypeReferenceHandle)typeDefOrRefOrSpec);
                HandleKind    handleType = typeRef.ResolutionScope.Kind;

                if (handleType == HandleKind.TypeReference || handleType == HandleKind.TypeDefinition)
                {
                    // Nested type
                    return(false);
                }

                return(reader.StringComparer.Equals(typeRef.Name, name) &&
                       reader.StringComparer.Equals(typeRef.Namespace, name));

            case HandleKind.TypeDefinition:
                TypeDefinition typeDef = reader.GetTypeDefinition((TypeDefinitionHandle)typeDefOrRefOrSpec);

                if (EcmaMetadataHelpers.IsNested(typeDef.Attributes))
                {
                    // Nested type
                    return(false);
                }

                return(reader.StringComparer.Equals(typeDef.Name, name) &&
                       reader.StringComparer.Equals(typeDef.Namespace, name));

            case HandleKind.TypeSpecification:
                // Generic attribute
                return(false);

            default:
                // unsupported metadata
                throw new BadImageFormatException();
            }
        }
        internal sealed override IEnumerable <ConstructorInfo> CoreGetDeclaredConstructors(NameFilter optionalNameFilter, RuntimeTypeInfo contextTypeInfo)
        {
            //
            // - It may sound odd to get a non-null name filter for a constructor search, but Type.GetMember() is an api that does this.
            //
            // - All GetConstructor() apis act as if BindingFlags.DeclaredOnly were specified. So the ReflectedType will always be the declaring type and so is not passed to this method.
            //
            MetadataReader reader = Reader;

            foreach (MethodDefinitionHandle methodHandle in DeclaredMethodAndConstructorHandles)
            {
                MethodDefinition method = reader.GetMethodDefinition(methodHandle);

                if (!EcmaMetadataHelpers.IsConstructor(ref method, reader))
                {
                    continue;
                }

                if (optionalNameFilter == null || optionalNameFilter.Matches(method.Name, reader))
                {
                    yield return(RuntimePlainConstructorInfo <EcmaFormatMethodCommon> .GetRuntimePlainConstructorInfo(new EcmaFormatMethodCommon(methodHandle, this, contextTypeInfo)));
                }
            }
        }
        //
        // Used to split methods between DeclaredMethods and DeclaredConstructors.
        //
        public static bool IsConstructor(this MethodDefinitionHandle methodHandle, MetadataReader reader)
        {
            MethodDefinition method = reader.GetMethodDefinition(methodHandle);

            return(EcmaMetadataHelpers.IsConstructor(ref method, reader));
        }