示例#1
0
        // Return the attributes on this type of the given custom attribute type
        internal MiniCustomAttributeInfo[] GetCustomAttributeInfos(Type caReflectedType)
        {
            List <MiniCustomAttributeInfo> result = new List <MiniCustomAttributeInfo>();

            PEFileReader peFile = _assembly.PEFileReader;

            peFile.InitMetaData();
            MDTables metaData = peFile.MetaData;

            uint numRows = metaData.RowsInTable(MDTables.Tables.CustomAttribute);

            for (uint i = 0; i < numRows; i++)
            {
                metaData.SeekToRowOfTable(MDTables.Tables.CustomAttribute, i);

                // Format: Parent type token, CA type token (really the constructor method), value (index into blob heap)
                MetadataToken targetType = metaData.ReadMetadataToken(MDTables.Encodings.HasCustomAttribute);
                MetadataToken caType     = metaData.ReadMetadataToken(MDTables.Encodings.CustomAttributeType);
                byte[]        caBlob     = metaData.ReadBlob();

                if (targetType.Equals(this._mdToken))
                {
                    //Console.WriteLine("CA - Applied to: {0}  CA .ctor: {1}  Value: {2}", targetType, caType, value);
                    //Console.WriteLine("CA MD Tokens  Parent: {0}  Type: {1}", targetType.ToMDToken(), caType.ToMDToken());

                    // Ensure the custom attribute type is the type we expect
                    metaData.SeekToMDToken(caType);
                    String caTypeName = null, caNameSpace = null;
                    if (caType.Table != MDTables.Tables.MemberRef)
                    {
                        // Custom attribute was defined in the assembly we are currently inspecting?
                        // Ignore it.
                        System.Diagnostics.Contracts.Contract.Assert(caType.Table == MDTables.Tables.MethodDef);
                        continue;
                    }

                    MetadataToken customAttributeType = metaData.ReadMetadataToken(MDTables.Encodings.MemberRefParent);

                    //Console.WriteLine("   MemberRef: {0}  Type of MemberRef: {1}", caType.ToMDToken(), customAttributeType.ToMDToken());
                    metaData.SeekToMDToken(customAttributeType);
                    MetadataToken resolutionScope     = metaData.ReadMetadataToken(MDTables.Encodings.ResolutionScope);
                    caTypeName  = metaData.ReadString();
                    caNameSpace = metaData.ReadString();

                    if (caTypeName == caReflectedType.Name && caNameSpace == caReflectedType.Namespace)
                    {
                        MiniCustomAttributeInfo customAttributeInfo = ParseCustomAttribute(caBlob, caReflectedType);
                        result.Add(customAttributeInfo);
                    }
                }
            }
            return(result.ToArray());
        }
示例#2
0
        /// <summary>
        /// Compare IMetadataTokenProvider instances based on their metadata token and their
        /// assembly.
        /// </summary>
        /// <param name="self">The IMetadataTokenProvider instance where the method is applied.</param>
        /// <param name="other">The IMetadataTokenProvider instance to compare to</param>
        /// <returns>True if the metadata tokens and assembly are identical, False otherwise</returns>
        public static bool Equals(this IMetadataTokenProvider self, IMetadataTokenProvider other)
        {
            if (self == other)
            {
                return(true);
            }
            if (other == null)
            {
                return(self == null);
            }

            MetadataToken token = self.MetadataToken;

            if (!token.Equals(other.MetadataToken))
            {
                return(false);
            }

            // metadata token is unique per assembly
            AssemblyDefinition self_assembly = GetAssembly(self);

            if (self_assembly == null)
            {
                // special case for Namespace (where GetAssembly would return null)
                if (token.TokenType == NamespaceDefinition.NamespaceTokenType)
                {
                    return((self as NamespaceDefinition).Name == (other as NamespaceDefinition).Name);
                }
                else
                {
                    return(false);
                }
            }
            AssemblyDefinition other_assembly = GetAssembly(other);

            // compare assemblies tokens (but do not recurse)
            return(other == null ? false : self_assembly.MetadataToken.Equals(other_assembly.MetadataToken));
        }
示例#3
0
 public bool Equals(MetadataRecord other) =>
 m_token.Equals(other.m_token) && ReferenceEquals(m_tables, other.m_tables);
示例#4
0
 public bool Equals(TypeKey other)
 {
     return(ModuleName == other.ModuleName && Token.Equals(other.Token));
 }