Exemple #1
0
 public TypeNestings(MetadataTables tables)
 {
     ContractUtils.Requires(tables != null);
     _tables = tables;
     _mapping = new Dictionary<MetadataToken, List<MetadataToken>>();
     Populate();
 }
Exemple #2
0
        private static MetadataTables[] GetAsseblyMetadata(Assembly assembly) {
            if (_metadataCache == null) {
                _metadataCache = new Dictionary<Assembly, MetadataTables[]>();
            }

            lock (_metadataCache) {
                MetadataTables[] metadata;
                if (!_metadataCache.TryGetValue(assembly, out metadata)) {
                    var modules = assembly.GetModules(false);
                    metadata = new MetadataTables[modules.Length];
                    int i = 1;
                    foreach (var module in modules) {
                        var tables = MetadataTables.OpenModule(module);
                        if (tables.AssemblyDef.Record.IsNull) {
                            metadata[i++] = MetadataTables.OpenModule(module);
                        } else {
                            metadata[0] = tables;
                        }
                    }
                    
                    _metadataCache.Add(assembly, metadata);
                }
                return metadata;
            }
        }
Exemple #3
0
        private static MetadataTables[] GetAsseblyMetadata(Assembly assembly)
        {
            if (_metadataCache == null) {
                _metadataCache = new Dictionary<Assembly, MetadataTables[]>();
            }

            lock (_metadataCache) {
                MetadataTables[] metadata;
                if (!_metadataCache.TryGetValue(assembly, out metadata)) {
                    var modules = assembly.GetModules(false);
                    metadata = new MetadataTables[modules.Length];
                    int i = 1;
                    foreach (var module in modules) {
                        var tables = MetadataTables.OpenModule(module);
                        if (tables.AssemblyDef.Record.IsNull) {
                            metadata[i++] = MetadataTables.OpenModule(module);
                        } else {
                            metadata[0] = tables;
                        }
                    }

                    _metadataCache.Add(assembly, metadata);
                }
                return metadata;
            }
        }
Exemple #4
0
        public static List<KeyValuePair<Module, int>> GetVisibleExtensionMethods(Assembly assembly) {
            if (assembly == null) {
                throw new ArgumentNullException("assembly");
            }

            MetadataTables manifest = GetAsseblyMetadata(assembly)[0];
            MetadataRecord eaCtor = GetExtensionAttributeCtor(manifest);
            var result = new List<KeyValuePair<Module, int>>();
            if (!eaCtor.IsNull) {
                foreach (CustomAttributeDef ca in manifest.CustomAttributes) {
                    if (ca.Constructor.Equals(eaCtor) && ca.Parent.IsMethodDef) {
                        MethodDef mdef = ca.Parent.MethodDef;
                        var mattrs = mdef.Attributes;
                        if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public && (mattrs & MethodAttributes.Static) != 0) {
                            var declType = mdef.FindDeclaringType();
                            var tattrs = declType.Attributes;
                            if (((tattrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public ||
                                (tattrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) &&
                                (tattrs & TypeAttributes.Abstract) != 0 &&
                                (tattrs & TypeAttributes.Sealed) != 0) {
                                result.Add(new KeyValuePair<Module, int>(manifest.Module, mdef.Record.Token.Value));
                            }
                        }
                    }
                }
            }
            return result;
        }
Exemple #5
0
 private static MetadataRecord GetExtensionAttributeCtor(MetadataTables tables) {
     AssemblyDef adef = tables.AssemblyDef;
     if (!adef.Record.IsNull) {
         foreach (CustomAttributeDef ca in adef.CustomAttributes) {
             if (IsExtensionAttribute(ca)) {
                 return ca.Constructor;
             }
         }
     }
     return default(MetadataRecord);
 }
        internal MetadataTableEnumerator(MetadataRecord parent, MetadataTokenType type)
        {
            Debug.Assert(parent.IsValid);

            m_type = type;
            m_tables = parent.m_tables;

            int start, count;
            m_indirection = parent.m_tables.m_import.GetEnumeratorRange(type, parent.Token, out start, out count);

            m_startRid = start;
            m_endRid = start + count;
            m_currentRid = start - 1;
        }
        internal MetadataTableEnumerator(MetadataRecord parent, MetadataTokenType type)
        {
            Debug.Assert(parent.IsValid);

            m_type   = type;
            m_tables = parent.m_tables;

            int start, count;

            m_indirection = parent.m_tables.m_import.GetEnumeratorRange(type, parent.Token, out start, out count);

            m_startRid   = start;
            m_endRid     = start + count;
            m_currentRid = start - 1;
        }
Exemple #8
0
        public static void DumpModule(MetadataTables tables)
        {
            ModuleDef md = tables.ModuleDef;
            _output.WriteLine("Module:");
            _output.WriteLine("  {0} {1}", md.Name, md.Mvid);

            AssemblyDef adef = tables.AssemblyDef;
            if (!adef.Record.IsNull) {
                _output.WriteLine("AssemblyDef:");
                _output.WriteLine("  {0} {1} {2} {3} {4} {5}",
                    adef.Name, adef.Version, adef.Culture, adef.NameFlags, adef.HashAlgorithm, BitConverter.ToString(adef.GetPublicKey())
                );
            }

            if (tables.AssemblyRefs.GetCount() > 0) {
                _output.WriteLine("AssemblyRefs:");
                foreach (AssemblyRef a in tables.AssemblyRefs) {
                    _output.WriteLine("  {0} {1} {2} {3} {5}",
                        a.Name, a.Version, a.Culture, a.NameFlags, BitConverter.ToString(a.GetHashValue()), BitConverter.ToString(a.GetPublicKeyOrToken())
                    );
                }
            }

            if (tables.Files.GetCount() > 0) {
                _output.WriteLine("Files:");
                foreach (FileDef fd in tables.Files) {
                    _output.WriteLine("  {0} {1} {2}",
                        fd.Name, fd.Attributes, BitConverter.ToString(fd.GetHashValue())
                    );
                }
            }

            if (tables.ManifestResources.GetCount() > 0) {
                _output.WriteLine("ManifestResources:");
                foreach (ManifestResourceDef resource in tables.ManifestResources) {
                    _output.WriteLine("  {0} {1} {2} 0x{3:X8}",
                        resource.Name, resource.Attributes, ImplementationToString(resource.Implementation), resource.Offset
                    );
                }
            }

            _output.WriteLine(new String('-', 50));
        }
Exemple #9
0
        public void Add(MetadataTables tables)
        {
            ContractUtils.Requires(tables != null);

            foreach (TypeDef typeDef in tables.TypeDefs) {
                if (typeDef.IsGlobal || typeDef.Attributes.IsNested()) {
                    continue;
                }

                MetadataNamePart prefix = typeDef.Namespace.GetExtent();
                NamespaceTreeNode ns = null;

                while (true) {
                    NamespaceTreeNode existing;
                    if (_names.TryGetValue(prefix, out existing)) {
                        if (ns == null) {
                            existing.AddType(typeDef);
                        } else {
                            existing.AddNamespace(ns);
                        }
                        break;
                    }

                    ContractUtils.Assert(prefix.Length > 0);
                    int lastDot = prefix.LastIndexOf((byte)'.', prefix.Length - 1, prefix.Length);

                    MetadataNamePart name = (lastDot >= 0) ? prefix.GetPart(lastDot + 1) : prefix;
                    NamespaceTreeNode newNs = new NamespaceTreeNode(name);
                    if (ns == null) {
                        newNs.AddType(typeDef);
                    } else {
                        newNs.AddNamespace(ns);
                    }
                    ns = newNs;

                    _names.Add(prefix, ns);

                    prefix = (lastDot >= 0) ? prefix.GetPart(0, lastDot) : MetadataNamePart.Empty;
                }
            }
        }
Exemple #10
0
 private static void ModuleEnumerated(MetadataTables tables)
 {
     #if CCI
     if (tables != null) {
         int mapSize = tables.GetRowCount(0x12) + tables.GetRowCount(0x15);
         Counter_MaxMapTablesSize = Math.Max(Counter_MaxMapTablesSize, mapSize);
         Counter_TotalMapTablesSize += mapSize;
     }
     #endif
     Counter_Files++;
 }
Exemple #11
0
 private static void DumpExtensionMethods(MetadataTables tables)
 {
     _output.WriteLine("Extension methods:");
     foreach (MethodDef em in tables.GetVisibleExtensionMethods()) {
         TypeDef td = em.FindDeclaringType();
         _output.WriteLine("{0}.{1}::{2}", td.Namespace, td.Name, em.Name);
     }
 }
Exemple #12
0
 private static void DumpMembers(MetadataTables tables)
 {
     foreach (TypeDef typeDef in tables.TypeDefs) {
         DumpMembers(typeDef);
     }
 }
Exemple #13
0
        private static void DumpNestedTypes(MetadataTables tables)
        {
            _output.WriteLine("Type nesting: ");

            TypeNestings nesting = new TypeNestings(tables);

            _output.WriteLine("Total pairs: {0}",
                (from e in nesting.GetEnclosingTypes()
                 from n in nesting.GetNestedTypes(e)
                 select 1).Sum()
            );

            DumpNestedTypes(nesting, 0,
                // top-level:
                from TypeDef typeDef in nesting.GetEnclosingTypes()
                where !typeDef.Attributes.IsNested()
                select typeDef
            );
        }
Exemple #14
0
 private static MetadataRecord GetExtensionAttributeCtor(MetadataTables tables)
 {
     AssemblyDef adef = tables.AssemblyDef;
     if (!adef.Record.IsNull) {
         foreach (CustomAttributeDef ca in adef.CustomAttributes) {
             if (IsExtensionAttribute(ca)) {
                 return ca.Constructor;
             }
         }
     }
     return default(MetadataRecord);
 }
Exemple #15
0
 private static void EnumerateExtensionMethods(MetadataTables tables)
 {
     foreach (MethodDef em in tables.GetVisibleExtensionMethods()) {
         if (tables.Module != null) {
             var mb = tables.Module.ResolveMethod(em.Record.Token.Value);
         }
         Counter_ExtensionMethods++;
     }
 }
 internal static MetadataRecord Null(MetadataTables tables)
 {
     return(new MetadataRecord(new MetadataToken(0), tables));
 }
Exemple #17
0
 private static string MdTablesToString(MetadataTables tables)
 {
     return tables.Module != null ? tables.Module.Assembly.ToString() + ":" + tables.Module.ToString() : tables.Path;
 }
Exemple #18
0
        private static void EnumerateMembers(MetadataTables tables)
        {
            foreach (TypeDef typeDef in tables.TypeDefs) {
                foreach (FieldDef fieldDef in typeDef.Fields) {
                    Counter_Fields++;
                }

                foreach (MethodDef methodDef in typeDef.Methods) {
                    Counter_Methods++;
                }

                foreach (PropertyDef propertyDef in typeDef.Properties) {
                    Counter_Properties++;
                }

                foreach (EventDef eventDef in typeDef.Events) {
                    Counter_Events++;
                }
            }
        }
 public static MetadataTables GetMetadataTables(this Module module)
 {
     return(MetadataTables.OpenFile(module.FullyQualifiedName));
 }
 internal MetadataRecord(MetadataToken token, MetadataTables tables)
 {
     Contract.Assert(tables != null);
     m_token  = token;
     m_tables = tables;
 }