Ejemplo n.º 1
0
        public Assembly Load(AssemblyName assemblyName)
        {
            // searchPaths could be in the following patterns
            // C:\HelloWorld\bin\HelloWorld.dll or
            // C:\HelloWorld\bin\HelloWorld.exe
            // C:\HelloWorld\bin\fr-FR\HelloWorld.resources.dll
            // C:\HelloWorld\bin\fr-FR\HelloWorld.resources.exe
            foreach (var searchPath in _searchPaths)
            {
                var path = searchPath;
                if (!ResourcesHelper.IsResourceNeutralCulture(assemblyName))
                {
                    path = Path.Combine(path, assemblyName.CultureName);
                }

                foreach (var extension in _extensions)
                {
                    var filePath = Path.Combine(path, assemblyName.Name + extension);

                    if (File.Exists(filePath))
                    {
                        return(_loadContext.LoadFile(filePath));
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
        public Assembly Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext)
        {
            string path;
            var    newAssemblyName = new AssemblyName(assemblyName.Name);

#if DNXCORE50
            newAssemblyName.CultureName = assemblyName.CultureName;
#elif DNX451
            // Assigning empty CultureInfo makes the new assembly culture as neutral which won't match the entries in _assemblies dictionary. Hence this check.
            if (assemblyName.CultureInfo != null && !ResourcesHelper.IsResourceNeutralCulture(assemblyName))
            {
                newAssemblyName.CultureInfo = assemblyName.CultureInfo;
            }
#else
#error Unhandled framework error
#endif
            if (_assemblies.TryGetValue(newAssemblyName, out path))
            {
                return(loadContext.LoadFile(path));
            }

            return(null);
        }
Ejemplo n.º 3
0
        public Assembly Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext)
        {
            using (var pdbStream = new MemoryStream())
                using (var assemblyStream = new MemoryStream())
                {
                    var afterCompileContext = new AfterCompileContext
                    {
                        ProjectContext = CompilationContext.ProjectContext,
                        Compilation    = CompilationContext.Compilation,
                        AssemblyStream = assemblyStream,
                        SymbolStream   = pdbStream
                    };

                    EmitResult emitResult = null;

                    // If assembly is not a satelite assembly or if assembly culture is neutral, then do not generate a resources assembly.
                    if (!string.Equals(Path.GetExtension(assemblyName.Name), ".resources") || ResourcesHelper.IsResourceNeutralCulture(assemblyName))
                    {
                        var resourcesForCulture = ResourcesForCulture.GetResourcesForCulture(assemblyName.CultureName ?? string.Empty, CompilationContext.Resources);
                        if (resourcesForCulture == null)
                        {
                            // No resources is fine for a main assembly
                            resourcesForCulture = Enumerable.Empty <ResourceDescriptor>();
                        }
                        var resources = resourcesForCulture
                                        .Select(res => new ResourceDescription(res.Name, res.StreamFactory, isPublic: true));

                        Logger.TraceInformation("[{0}]: Emitting assembly for {1}", GetType().Name, Name);

                        var sw = Stopwatch.StartNew();

                        bool emitPdb;
                        var  emitOptions = GetEmitOptions(out emitPdb);
                        emitResult = CompilationContext.Compilation.Emit(assemblyStream,
                                                                         pdbStream: emitPdb ? pdbStream : null,
                                                                         manifestResources: resources,
                                                                         options: emitOptions);

                        sw.Stop();

                        Logger.TraceInformation("[{0}]: Emitted {1} in {2}ms", GetType().Name, Name, sw.ElapsedMilliseconds);

                        foreach (var m in CompilationContext.Modules)
                        {
                            m.AfterCompile(afterCompileContext);
                        }
                    }
                    else
                    {
                        var resourcesForCulture = ResourcesForCulture.GetResourcesForCulture(assemblyName.CultureName ?? string.Empty, CompilationContext.Resources);
                        if (resourcesForCulture == null)
                        {
                            return(null);
                        }
                        afterCompileContext.SymbolStream = null;
                        emitResult = EmitResourceAssembly(assemblyName, resourcesForCulture, afterCompileContext.Compilation.Options, afterCompileContext.AssemblyStream);
                    }

                    afterCompileContext.Diagnostics = CompilationContext.Diagnostics.Concat(emitResult.Diagnostics).ToList();

                    if (!emitResult.Success || afterCompileContext.Diagnostics.Any(RoslynDiagnosticUtilities.IsError))
                    {
                        throw new RoslynCompilationException(afterCompileContext.Diagnostics, CompilationContext.ProjectContext.TargetFramework);
                    }

                    Assembly assembly = null;

                    // If this is null it'll fail anyways, just don't blow up with
                    // a null reference
                    if (afterCompileContext.AssemblyStream != null)
                    {
                        afterCompileContext.AssemblyStream.Position = 0;
                    }

                    if (afterCompileContext.SymbolStream == null ||
                        afterCompileContext.SymbolStream.Length == 0)
                    {
                        assembly = loadContext.LoadStream(afterCompileContext.AssemblyStream, assemblySymbols: null);
                    }
                    else
                    {
                        afterCompileContext.SymbolStream.Position = 0;

                        assembly = loadContext.LoadStream(afterCompileContext.AssemblyStream, afterCompileContext.SymbolStream);
                    }

                    return(assembly);
                }
        }