Contains resolved types and methods from the common Stiletto library.
Example #1
0
        private void ImportStilettoReferences(ModuleDefinition module, StilettoReferences stilettoReferences)
        {
            Binding                  = module.Import(stilettoReferences.Binding);
            Binding_Ctor             = module.Import(stilettoReferences.Binding_Ctor);
            Binding_GetDependencies  = module.Import(stilettoReferences.Binding_GetDependencies);
            Binding_Resolve          = module.Import(stilettoReferences.Binding_Resolve);
            Binding_Get              = module.Import(stilettoReferences.Binding_Get);
            Binding_InjectProperties = module.Import(stilettoReferences.Binding_InjectProperties);
            Binding_RequiredByGetter = module.Import(stilettoReferences.Binding_RequiredBy_Getter);
            Binding_IsLibrarySetter  = module.Import(stilettoReferences.Binding_IsLibrary_Setter);

            BindingArray = new ArrayType(Binding);

            SetBindings        = module.Import(stilettoReferences.SetBindings);
            SetBindings_AddOfT = module.Import(stilettoReferences.SetBindings_Add);

            RuntimeModule              = module.Import(stilettoReferences.RuntimeModule);
            RuntimeModule_Ctor         = module.Import(stilettoReferences.RuntimeModule_Ctor);
            RuntimeModule_ModuleGetter = module.Import(stilettoReferences.RuntimeModule_Module_Getter);

            Container                   = module.Import(stilettoReferences.Container);
            Container_Create            = module.Import(stilettoReferences.Container_Create);
            Container_CreateWithLoaders = module.Import(stilettoReferences.Container_CreateWithLoaders);

            ILoader = module.Import(stilettoReferences.ILoader);
            ILoader_GetInjectBinding          = module.Import(stilettoReferences.ILoader_GetInjectBinding);
            ILoader_GetLazyInjectBinding      = module.Import(stilettoReferences.ILoader_GetLazyInjectBinding);
            ILoader_GetIProviderInjectBinding = module.Import(stilettoReferences.ILoader_GetIProviderInjectBinding);
            ILoader_GetRuntimeModue           = module.Import(stilettoReferences.ILoader_GetRuntimeModue);

            IProviderOfT     = module.Import(stilettoReferences.IProviderOfT);
            IProviderOfT_Get = module.Import(stilettoReferences.IProviderOfT_Get);

            Resolver = module.Import(stilettoReferences.Resolver);
            Resolver_RequestBinding = module.Import(stilettoReferences.Resolver_RequestBinding);

            InjectAttribute    = module.Import(stilettoReferences.InjectAttribute);
            ModuleAttribute    = module.Import(stilettoReferences.ModuleAttribute);
            ProvidesAttribute  = module.Import(stilettoReferences.ProvidesAttribute);
            NamedAttribute     = module.Import(stilettoReferences.NamedAttribute);
            SingletonAttribute = module.Import(stilettoReferences.SingletonAttribute);

            ProcessedAssemblyAttribute      = module.Import(stilettoReferences.ProcessedAssemblyAttribute);
            ProcessedAssemblyAttribute_Ctor = module.Import(stilettoReferences.ProcessedAssemblyAttribute_Ctor);
        }
Example #2
0
        private void ImportStilettoReferences(ModuleDefinition module, StilettoReferences stilettoReferences)
        {
            Binding = module.Import(stilettoReferences.Binding);
            Binding_Ctor = module.Import(stilettoReferences.Binding_Ctor);
            Binding_GetDependencies = module.Import(stilettoReferences.Binding_GetDependencies);
            Binding_Resolve = module.Import(stilettoReferences.Binding_Resolve);
            Binding_Get = module.Import(stilettoReferences.Binding_Get);
            Binding_InjectProperties = module.Import(stilettoReferences.Binding_InjectProperties);
            Binding_RequiredByGetter = module.Import(stilettoReferences.Binding_RequiredBy_Getter);
            Binding_IsLibrarySetter = module.Import(stilettoReferences.Binding_IsLibrary_Setter);

            BindingArray = new ArrayType(Binding);

            ProviderMethodBindingBase = module.Import(stilettoReferences.ProviderMethodBindingBase);
            ProviderMethodBindingBase_Ctor = module.Import(stilettoReferences.ProviderMethodBindingBase_Ctor);

            RuntimeModule = module.Import(stilettoReferences.RuntimeModule);
            RuntimeModule_Ctor = module.Import(stilettoReferences.RuntimeModule_Ctor);
            RuntimeModule_ModuleGetter = module.Import(stilettoReferences.RuntimeModule_Module_Getter);

            Container = module.Import(stilettoReferences.Container);
            Container_Create = module.Import(stilettoReferences.Container_Create);
            Container_CreateWithPlugins = module.Import(stilettoReferences.Container_CreateWithPlugins);

            IPlugin = module.Import(stilettoReferences.IPlugin);
            IPlugin_GetInjectBinding = module.Import(stilettoReferences.IPlugin_GetInjectBinding);
            IPlugin_GetLazyInjectBinding = module.Import(stilettoReferences.IPlugin_GetLazyInjectBinding);
            IPlugin_GetIProviderInjectBinding = module.Import(stilettoReferences.IPlugin_GetIProviderInjectBinding);
            IPlugin_GetRuntimeModue = module.Import(stilettoReferences.IPlugin_GetRuntimeModue);

            IProviderOfT = module.Import(stilettoReferences.IProviderOfT);
            IProviderOfT_Get = module.Import(stilettoReferences.IProviderOfT_Get);

            Resolver = module.Import(stilettoReferences.Resolver);
            Resolver_RequestBinding = module.Import(stilettoReferences.Resolver_RequestBinding);

            InjectAttribute = module.Import(stilettoReferences.InjectAttribute);
            ModuleAttribute = module.Import(stilettoReferences.ModuleAttribute);
            ProvidesAttribute = module.Import(stilettoReferences.ProvidesAttribute);
            NamedAttribute = module.Import(stilettoReferences.NamedAttribute);
            SingletonAttribute = module.Import(stilettoReferences.SingletonAttribute);

            ProcessedAssemblyAttribute = module.Import(stilettoReferences.ProcessedAssemblyAttribute);
            ProcessedAssemblyAttribute_Ctor = module.Import(stilettoReferences.ProcessedAssemblyAttribute_Ctor);
        }
Example #3
0
        public References(ModuleDefinition module, StilettoReferences stilettoReferences)
        {
            ImportStilettoReferences(module, stilettoReferences);

            Void = module.TypeSystem.Void;
            Object = module.TypeSystem.Object;
            String = module.TypeSystem.String;
            Boolean = module.TypeSystem.Boolean;

            var assemblyResolver = module.AssemblyResolver;
            var mscorlib = assemblyResolver.Resolve("mscorlib");
            var mscorlibTypes = mscorlib.MainModule.Types;

            var system = assemblyResolver.Resolve("System");
            var systemTypes = system.MainModule.Types;

            var tDict = module.Import(mscorlibTypes.First(t => t.Name == "Dictionary`2"));

            FuncOfT = module.Import(mscorlibTypes.First(t => t.Name == "Func`1"));
            FuncOfT4 = module.Import(mscorlibTypes.First(t => t.Name == "Func`4"));
            FuncOfT5 = module.Import(mscorlibTypes.First(t => t.Name == "Func`5"));
            LazyOfT = module.Import(mscorlibTypes.First(t => t.Name == "Lazy`1"));

            var compilerGeneratedAttribute = mscorlibTypes.First(t => t.Name == "CompilerGeneratedAttribute");
            var compilerGeneratedAttributeCtor = compilerGeneratedAttribute.GetMethod(".ctor");
            CompilerGeneratedAttribute = module.Import(compilerGeneratedAttributeCtor);

            Type = module.Import(mscorlibTypes.First(t => t.Name == "Type"));
            Type_GetTypeFromHandle = module.Import(Type.Resolve().GetMethod("GetTypeFromHandle"));

            var tSetOfBindings = module.Import(systemTypes.First(t => t.Name == "ISet`1")).MakeGenericInstanceType(Binding);
            SetOfBindings = module.Import(tSetOfBindings);
            SetOfBindings_Add = module.Import(tSetOfBindings.Resolve().GetMethod("Add")).MakeHostInstanceGeneric(Binding);
            SetOfBindings_UnionWith = module.Import(tSetOfBindings.Resolve().GetMethod("UnionWith")).MakeHostInstanceGeneric(Binding);

            // Used in RuntimeModule.GetBindings(IDictionary<string, Binding>);
            var tDictOfStringToBinding = module
                .Import(mscorlibTypes.First(t => t.Name == "IDictionary`2"))
                .MakeGenericInstanceType(String, Binding);

            DictionaryOfStringToBinding = tDictOfStringToBinding;
            DictionaryOfStringToBinding_Add = module.Import(tDictOfStringToBinding.Resolve().GetMethod("Add"))
                .MakeHostInstanceGeneric(String, Binding);

            // Used in $CompiledPlugin$::.ctor()
            ImportInjectBindingDictionary(module, tDict);

            // Used in $CompiledPlugin$::.ctor()
            ImportLazyBindingDictionary(module, tDict);

            // Used in $CompiledPlugin$::.ctor()
            ImportProviderBindingDictionary(module, tDict);

            // Used in $CompiledPlugin$::.ctor()
            ImportRuntimeModuleDictionary(module, tDict);

            var stringComparer = mscorlibTypes.First(t => t.Name == "StringComparer");
            var ordinalProperty = stringComparer.GetProperty("Ordinal");
            var ordinalPropertyGetter = ordinalProperty.GetMethod;
            StringComparer_Ordinal_Getter = module.Import(ordinalPropertyGetter);
        }
Example #4
0
        public References(ModuleDefinition module, StilettoReferences stilettoReferences)
        {
            ImportStilettoReferences(module, stilettoReferences);

            Void    = module.TypeSystem.Void;
            Object  = module.TypeSystem.Object;
            String  = module.TypeSystem.String;
            Boolean = module.TypeSystem.Boolean;

            var assemblyResolver = module.AssemblyResolver;
            var mscorlib         = assemblyResolver.Resolve("mscorlib");
            var mscorlibTypes    = mscorlib.MainModule.Types;

            var system      = assemblyResolver.Resolve("System");
            var systemTypes = system.MainModule.Types;

            var tDict = module.Import(mscorlibTypes.First(t => t.Name == "Dictionary`2"));

            FuncOfT  = module.Import(mscorlibTypes.First(t => t.Name == "Func`1"));
            FuncOfT4 = module.Import(mscorlibTypes.First(t => t.Name == "Func`4"));
            FuncOfT5 = module.Import(mscorlibTypes.First(t => t.Name == "Func`5"));
            LazyOfT  = module.Import(mscorlibTypes.First(t => t.Name == "Lazy`1"));

            var compilerGeneratedAttribute     = mscorlibTypes.First(t => t.Name == "CompilerGeneratedAttribute");
            var compilerGeneratedAttributeCtor = compilerGeneratedAttribute.GetMethod(".ctor");

            CompilerGeneratedAttribute = module.Import(compilerGeneratedAttributeCtor);

            var internalsVisibleTo     = mscorlibTypes.First(t => t.Name == "InternalsVisibleToAttribute");
            var internalsVisibleToCtor = internalsVisibleTo.GetConstructors().First(c => c.Parameters.Count == 1);

            InternalsVisibleToAttribute = module.Import(internalsVisibleToCtor);

            Type = module.Import(mscorlibTypes.First(t => t.Name == "Type"));
            Type_GetTypeFromHandle = module.Import(Type.Resolve().GetMethod("GetTypeFromHandle"));

            var tSetOfBindings = module.Import(systemTypes.First(t => t.Name == "ISet`1")).MakeGenericInstanceType(Binding);

            SetOfBindings           = module.Import(tSetOfBindings);
            SetOfBindings_Add       = module.Import(tSetOfBindings.Resolve().GetMethod("Add")).MakeHostInstanceGeneric(Binding);
            SetOfBindings_UnionWith = module.Import(tSetOfBindings.Resolve().GetMethod("UnionWith")).MakeHostInstanceGeneric(Binding);

            // Used in RuntimeModule.GetBindings(IDictionary<string, Binding>);
            var tDictOfStringToBinding = module
                                         .Import(mscorlibTypes.First(t => t.Name == "IDictionary`2"))
                                         .MakeGenericInstanceType(String, Binding);

            DictionaryOfStringToBinding     = tDictOfStringToBinding;
            DictionaryOfStringToBinding_Add = module.Import(tDictOfStringToBinding.Resolve().GetMethod("Add"))
                                              .MakeHostInstanceGeneric(String, Binding);

            // Used in $CompiledLoader$::.ctor()
            ImportInjectBindingDictionary(module, tDict);

            // Used in $CompiledLoader$::.ctor()
            ImportLazyBindingDictionary(module, tDict);

            // Used in $CompiledLoader$::.ctor()
            ImportProviderBindingDictionary(module, tDict);

            // Used in $CompiledLoader$::.ctor()
            ImportRuntimeModuleDictionary(module, tDict);

            var stringComparer        = mscorlibTypes.First(t => t.Name == "StringComparer");
            var ordinalProperty       = stringComparer.GetProperty("Ordinal");
            var ordinalPropertyGetter = ordinalProperty.GetMethod;

            StringComparer_Ordinal_Getter = module.Import(ordinalPropertyGetter);
        }
Example #5
0
        private IList <ModuleProcessor> GatherModulesNeedingProcessing()
        {
            var processors = new List <ModuleProcessor>();

            if (!IsModuleProcessable(ModuleDefinition))
            {
                return(processors);
            }

            var stilettoReferences  = StilettoReferences.Create(AssemblyResolver);
            var mainModuleProcessor = new ModuleProcessor(errorReporter, ModuleDefinition, stilettoReferences);

            processors.Add(mainModuleProcessor);
            AddModuleToAssemblyDictionary(ModuleDefinition, mainModuleProcessor);

            var copyLocalAssemblies = new Dictionary <string, bool>(StringComparer.Ordinal);
            var localDebugFiles     = new Queue <string>();

            foreach (var copyLocal in ReferenceCopyLocalPaths)
            {
                if (copyLocal.EndsWith(".pdb") || copyLocal.EndsWith(".mdb"))
                {
                    // We'll come back to the debug files after we have a complete
                    // list of local assemblies.
                    localDebugFiles.Enqueue(copyLocal);
                    continue;
                }

                if (copyLocal.EndsWith(".exe") || copyLocal.EndsWith(".dll"))
                {
                    copyLocalAssemblies[copyLocal] = false;
                }
            }

            // Check which assemblies have debug symbols and, consequently,
            // for which assemblies we will attempt to read and write such symbols.
            while (localDebugFiles.Count > 0)
            {
                var pdb     = localDebugFiles.Dequeue();
                var rawPath = Path.Combine(Path.GetDirectoryName(pdb), Path.GetFileNameWithoutExtension(pdb));
                var dll     = rawPath + ".dll";
                var exe     = rawPath + ".exe";

                if (copyLocalAssemblies.ContainsKey(dll))
                {
                    copyLocalAssemblies[dll] = true;
                }

                if (copyLocalAssemblies.ContainsKey(exe))
                {
                    copyLocalAssemblies[exe] = true;
                }
            }

            foreach (var pathAndHasPdb in copyLocalAssemblies)
            {
                var path     = pathAndHasPdb.Key;
                var hasPdb   = pathAndHasPdb.Value;
                var assembly = AssemblyDefinition.ReadAssembly(path, new ReaderParameters {
                    ReadSymbols = hasPdb
                });

                // TODO: Figure out how to differentiate between third-party libs and client code.
                if (assembly.Name.HasPublicKey)
                {
                    LogWarning("Assembly " + assembly.Name + " is strong-named and will not be processed.");
                    continue;
                }

                dependencies[path] = Tuple.Create(assembly, hasPdb);

                foreach (var module in assembly.Modules)
                {
                    if (!IsModuleProcessable(module))
                    {
                        continue;
                    }

                    var moduleProcessor = new ModuleProcessor(errorReporter, module, stilettoReferences);
                    processors.Add(moduleProcessor);
                    AddModuleToAssemblyDictionary(module, moduleProcessor);
                }
            }

            return(processors);
        }
Example #6
0
        public ModuleProcessor(IErrorReporter errorReporter, ModuleDefinition moduleDefinition, StilettoReferences stilettoReferences)
        {
            this.errorReporter    = Conditions.CheckNotNull(errorReporter, "errorReporter");
            this.moduleDefinition = Conditions.CheckNotNull(moduleDefinition, "moduleDefinition");

            references         = new References(moduleDefinition, stilettoReferences);
            moduleGenerators   = new List <ModuleGenerator>();
            injectGenerators   = new List <InjectBindingGenerator>();
            lazyGenerators     = new List <LazyBindingGenerator>();
            providerGenerators = new List <ProviderBindingGenerator>();
            baseGeneratorQueue = new Queue <TypeDefinition>();
        }