/// <summary>
        /// Returns the full set of <see cref="Assembly"/> instances from which to build the MEF composition container,
        /// </summary>
        /// <param name="compositionAssemblyPaths">Optional set of assembly locations to include.</param>
        /// <param name="logger"><see cref="ILogger"/> instance to report issues.</param>
        /// <returns>The set of <see cref="Assembly"/> instances to use.</returns>
        private static IEnumerable <Assembly> GetCompositionAssemblies(IEnumerable <string> compositionAssemblyPaths, ILogger logger)
        {
            HashSet <Assembly> assemblies = new HashSet <Assembly>();

            if (compositionAssemblyPaths != null)
            {
                foreach (string assemblyPath in compositionAssemblyPaths)
                {
                    Assembly a = AssemblyUtilities.LoadAssembly(assemblyPath, logger);
                    if (a != null)
                    {
                        // Don't put System assemblies into container
                        if (!a.IsSystemAssembly())
                        {
                            assemblies.Add(a);
                        }
                    }
                }
            }

            // Add this assembly itself to allow MEF to satisfy our imports
            assemblies.Add(typeof(ClientCodeGenerationDispatcher).Assembly);

            return(assemblies);
        }
        /// <summary>
        /// Looks at all loaded assemblies and adds EntityDescription for each entity found
        /// </summary>
        private void AddEntityDescriptions()
        {
            var entityDescription = new EntityDescription();

            foreach (KeyValuePair <Assembly, bool> pair in _loadedAssemblies)
            {
                // Performance optimization: standard Microsoft assemblies are excluded from this search
                if (pair.Value)
                {
                    // Utility autorecovers and logs for common exceptions
                    var types = AssemblyUtilities.GetExportedTypes(pair.Key, _logger);

                    foreach (var type in types)
                    {
                        try
                        {
                            entityDescription.TryAddEntityType(type);
                        }
                        catch (Exception e)
                        {
                            LogWarning(e.ToString());
                        }
                    }
                }
            }

            entityDescription.Initialize();

            if (entityDescription.EntityTypes.Any())
            {
                _entityDescriptions.Add(entityDescription);
            }
        }
Example #3
0
        /// <summary>
        /// Does a "reflection only load" of all the reference assemblies for the given <paramref name="assembly"/>
        /// </summary>
        /// <param name="assembly">The assembly whose references need to be loaded.</param>
        /// <param name="assemblySearchPaths">Optional list of folders to search for assembly references.</param>
        /// <param name="loadedAssemblies">Dictionary to track already loaded assemblies.</param>
        /// <param name="recursive">If <c>true</c> recursively load references from the references.</param>
        /// <param name="logger">The optional logger to use to report known load failures.</param>
        /// <returns><c>true</c> means all loads succeeded, <c>false</c> means some errors occurred and were logged.
        /// </returns>
        internal static bool ReflectionOnlyLoadReferences(Assembly assembly, IEnumerable <string> assemblySearchPaths, Dictionary <string, Assembly> loadedAssemblies, bool recursive, ILogger logger)
        {
            System.Diagnostics.Debug.Assert(assembly != null, "assembly is required");
            System.Diagnostics.Debug.Assert(loadedAssemblies != null, "loadedAssemblies is required");

            bool result = true;

            // Ensure this assembly itself is shown in our loaded list
            loadedAssemblies[assembly.FullName] = assembly;

            var assemblyReferences = assembly.GetReferencedAssemblies();

            foreach (var name in assemblyReferences)
            {
                // We cannot load MSCorLib into an RO load.
                // Attempting to load the SL version will only return the already loaded MSCorlib
                if (IsAssemblyMsCorlib(name))
                {
                    continue;
                }

                Assembly referenceAssembly;

                // It may have been loaded already.  If so, assume that means we already
                // followed down its references.  Otherwise, load it and honor the
                // request to recursively load its references.
                if (!loadedAssemblies.TryGetValue(name.FullName, out referenceAssembly))
                {
                    referenceAssembly = ReflectionOnlyLoad(name, assemblySearchPaths, logger);

                    // Note: we always put the result into our cache, even for failure.
                    // This prevents us from attempting to load it multiple times
                    loadedAssemblies[name.FullName] = referenceAssembly;

                    if (referenceAssembly != null && recursive)
                    {
                        if (!AssemblyUtilities.ReflectionOnlyLoadReferences(referenceAssembly, assemblySearchPaths, loadedAssemblies, recursive, logger))
                        {
                            result = false;
                        }
                    }
                    else
                    {
                        // failure to load anything give false return
                        result = false;
                    }
                }
            }
            return(result);
        }
        /// <summary>
        /// Invoked once to force load all assemblies into an analysis unit
        /// </summary>
        private void LoadAllAssembliesAndSetAssemblyResolver()
        {
            _loadedAssemblies = new Dictionary <Assembly, bool>();

            foreach (var assemblyName in _assembliesToLoad)
            {
                Assembly assembly = AssemblyUtilities.LoadAssembly(assemblyName, _logger);
                if (assembly != null)
                {
                    // The bool value indicates whether this assembly should be searched for a Entity
                    _loadedAssemblies[assembly] = !assembly.IsSystemAssembly();
                }
            }

            AssemblyUtilities.SetAssemblyResolver(_loadedAssemblies.Keys);
        }