Beispiel #1
0
        private bool IsWindowsAttributeUsageAttribute(EntityHandle targetType, CustomAttributeHandle attributeHandle)
        {
            // Check for Windows.Foundation.Metadata.AttributeUsageAttribute.
            // WinMD rules:
            //   - The attribute is only applicable on TypeDefs.
            //   - Constructor must be a MemberRef with TypeRef.

            if (targetType.Kind != HandleKind.TypeDefinition)
            {
                return(false);
            }

            var attributeCtor = CustomAttributeTable.GetConstructor(attributeHandle);

            if (attributeCtor.Kind != HandleKind.MemberReference)
            {
                return(false);
            }

            var attributeType = MemberRefTable.GetClass((MemberReferenceHandle)attributeCtor);

            if (attributeType.Kind != HandleKind.TypeReference)
            {
                return(false);
            }

            var attributeTypeRef = (TypeReferenceHandle)attributeType;

            return(StringStream.EqualsRaw(TypeRefTable.GetName(attributeTypeRef), "AttributeUsageAttribute") &&
                   StringStream.EqualsRaw(TypeRefTable.GetNamespace(attributeTypeRef), "Windows.Foundation.Metadata"));
        }
Beispiel #2
0
        /// <summary>
        /// Returns the type definition or reference handle of the attribute type.
        /// </summary>
        /// <returns><see cref="TypeDefinitionHandle"/> or <see cref="TypeReferenceHandle"/> or nil token if the metadata is invalid and the type can't be determined.</returns>
        private EntityHandle GetAttributeTypeRaw(CustomAttributeHandle handle)
        {
            var ctor = CustomAttributeTable.GetConstructor(handle);

            if (ctor.Kind == HandleKind.MethodDefinition)
            {
                return(GetDeclaringType((MethodDefinitionHandle)ctor));
            }

            if (ctor.Kind == HandleKind.MemberReference)
            {
                // In general the parent can be MethodDef, ModuleRef, TypeDef, TypeRef, or TypeSpec.
                // For attributes only TypeDef and TypeRef are applicable.
                EntityHandle typeDefOrRef = MemberRefTable.GetClass((MemberReferenceHandle)ctor);
                HandleKind   handleType   = typeDefOrRef.Kind;

                if (handleType == HandleKind.TypeReference || handleType == HandleKind.TypeDefinition)
                {
                    return(typeDefOrRef);
                }
            }

            return(default(EntityHandle));
        }