Example #1
0
 public ILAssemblyTranslator(string binaryPath, bool loadReferences, string[] searchPaths)
 {
     allAssemblies = new Stack <ILAssembly>();
     using (var assemblyResolver = new DefaultAssemblyResolver())
     {
         foreach (string path in searchPaths)
         {
             assemblyResolver.AddSearchDirectory(path);
         }
         assembly = new ILAssembly(allAssemblies, binaryPath, loadReferences, assemblyResolver);
     }
 }
Example #2
0
        public ILModule(Stack <ILAssembly> allAssemblies, ILAssembly assembly, bool loadReferences, ModuleDefinition moduleDefinition, ISymbolReader symbolReader, DefaultAssemblyResolver assemblyResolver)
        {
            this.assembly         = assembly;
            this.moduleDefinition = moduleDefinition;
            this.symbolReader     = symbolReader;

            // create dependency ordered type list
            typesDependencyOrdered = moduleDefinition.Types.ToList();
            typesDependencyOrdered.Sort(delegate(TypeDefinition x, TypeDefinition y)
            {
                if (x == y)
                {
                    return(0);
                }

                // if we're a struct, make sure we are put after any fields we depend on
                if (x.IsValueType)
                {
                    foreach (var field in x.Fields)
                    {
                        if (field.FieldType.FullName == y.FullName)
                        {
                            return(1);
                        }
                    }
                }

                // validate we are put in front of our base types
                var baseType = x.BaseType;
                while (baseType != null)
                {
                    if (!baseType.IsDefinition)
                    {
                        return(0);                                           // if our base type lives in a different module, ignore
                    }
                    if (baseType.FullName == y.FullName)
                    {
                        return(1);                                                    // make sure we are put after our base type
                    }
                    var baseTypeDef = (TypeDefinition)baseType;
                    baseType        = baseTypeDef.BaseType;
                }

                return(0);
            });

            // load references
            references = new Stack <ILAssembly>();
            if (loadReferences)
            {
                foreach (var nameReference in moduleDefinition.AssemblyReferences)
                {
                    using (var assemblyDefinition = assemblyResolver.Resolve(nameReference))
                    {
                        var ilAssembly = allAssemblies.FirstOrDefault(x => x.assemblyDefinition.FullName == assemblyDefinition.FullName);
                        if (ilAssembly == null)
                        {
                            ilAssembly = new ILAssembly(allAssemblies, assemblyDefinition.MainModule.FileName, loadReferences, assemblyResolver);
                        }
                        references.Push(ilAssembly);
                    }
                }
            }
        }