コード例 #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"));
        }
コード例 #2
0
        /// <summary>
        /// We want to know if a given method implements a redirected interface.
        /// For example, if we are given the method RemoveAt on a class "A"
        /// which implements the IVector interface (which is redirected
        /// to IList in .NET) then this method would return true. The most
        /// likely reason why we would want to know this is that we wish to hide
        /// (mark private) all methods which implement methods on a redirected
        /// interface.
        /// </summary>
        /// <param name="memberRef">The declaration token for the method</param>
        /// <param name="isIDisposable">
        /// Returns true if the redirected interface is <see cref="IDisposable"/>.
        /// </param>
        /// <returns>True if the method implements a method on a redirected interface.
        /// False otherwise.</returns>
        private bool ImplementsRedirectedInterface(MemberReferenceHandle memberRef, out bool isIDisposable)
        {
            isIDisposable = false;

            EntityHandle parent = MemberRefTable.GetClass(memberRef);

            TypeReferenceHandle typeRef;

            if (parent.Kind == HandleKind.TypeReference)
            {
                typeRef = (TypeReferenceHandle)parent;
            }
            else if (parent.Kind == HandleKind.TypeSpecification)
            {
                BlobHandle blob = TypeSpecTable.GetSignature((TypeSpecificationHandle)parent);
                BlobReader sig  = new BlobReader(BlobStream.GetMemoryBlock(blob));

                if (sig.Length < 2 ||
                    sig.ReadByte() != (byte)CorElementType.ELEMENT_TYPE_GENERICINST ||
                    sig.ReadByte() != (byte)CorElementType.ELEMENT_TYPE_CLASS)
                {
                    return(false);
                }

                EntityHandle token = sig.ReadTypeHandle();
                if (token.Kind != HandleKind.TypeReference)
                {
                    return(false);
                }

                typeRef = (TypeReferenceHandle)token;
            }
            else
            {
                return(false);
            }

            return(GetProjectionIndexForTypeReference(typeRef, out isIDisposable) >= 0);
        }
コード例 #3
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));
        }