Ejemplo n.º 1
0
        /// <summary>
        /// Generates an assembly from a source and a minimal list of required reference assemblies.
        /// </summary>
        /// <param name="code">The source code.</param>
        /// <param name="assemblyPath">The full final assembly path (including the .dll extension). Can be null if skipCompilation is true.</param>
        /// <param name="skipCompilation">True to skip the compilation. Only the parsing and the source generation is done.</param>
        /// <param name="loader">Optional loader function to load the final emitted assembly.</param>
        /// <returns>Encapsulation of the result.</returns>
        public GenerateResult Generate(ICodeWorkspace code, string assemblyPath, bool skipCompilation, Func <string, Assembly>?loader = null)
        {
            if (code == null)
            {
                throw new ArgumentNullException(nameof(code));
            }
            using (var weakLoader = WeakAssemblyNameResolver.TemporaryInstall())
            {
                var input = GeneratorInput.Create(_workspaceFactory, code, Modules, !skipCompilation && AutoRegisterRuntimeAssembly, ParseOptions);
                Modules.Clear();

                if (skipCompilation)
                {
                    return(new GenerateResult(input.Trees));
                }

                var collector = new HashSet <Assembly>();
                foreach (var a in input.Assemblies)
                {
                    if (collector.Add(a))
                    {
                        Discover(a, collector);
                    }
                }
                return(Generate(CompilationOptions,
                                input.Trees,
                                assemblyPath,
                                collector.Select(a => MetadataReference.CreateFromFile(new Uri(a.Location).LocalPath)),
                                loader)
                       .WithLoadFailures(weakLoader.Conflicts));
            }
        }
Ejemplo n.º 2
0
 public static ITestHelperResolver Create(ITestHelperConfiguration config = null)
 {
     using (WeakAssemblyNameResolver.TemporaryInstall())
     {
         return(new ResolverImpl(config ?? TestHelperConfiguration.Default));
     }
 }
Ejemplo n.º 3
0
 ResolverImpl(ITestHelperConfiguration config)
 {
     _container = new SimpleServiceContainer();
     _container.Add(config);
     _config       = config;
     TransientMode = config.GetBoolean("TestHelper/TransientMode") ?? false;
     string[] assemblies = config.Get("TestHelper/PreLoadedAssemblies", String.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
     if (assemblies.Length > 0)
     {
         using (WeakAssemblyNameResolver.TemporaryInstall())
         {
             var types = new List <Type>();
             foreach (var n in assemblies)
             {
                 var a = Assembly.Load(n);
                 types.AddRange(a.GetExportedTypes().Where(t => t.IsInterface && typeof(IMixinTestHelper).IsAssignableFrom(t)));
             }
             _preLoadedTypes = types;
             if (!TransientMode)
             {
                 var ctx = new Context(_container);
                 foreach (var preLoad in _preLoadedTypes)
                 {
                     Resolve(ctx, preLoad);
                 }
             }
         }
     }
     else
     {
         _preLoadedTypes = Type.EmptyTypes;
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Compiles or parses only a single code source.
 /// </summary>
 /// <param name="code"></param>
 /// <param name="assemblyPath">The output path or null if only parsing is required.</param>
 /// <param name="references">Optional list of dependent assemblies. Used only if compilation is required. This list will be transitively closed.</param>
 /// <param name="parseOptions">By default, all default applies, the language version is <see cref="LanguageVersion.Default"/>.</param>
 /// <param name="compileOptions">The compilation options. Used only if compilation is required. Defaults to <see cref="DefaultCompilationOptions"/>.</param>
 /// <param name="loader">Optional loader function to load the final emitted assembly. Used only if compilation is required.</param>
 /// <returns>Encapsulation of the result.</returns>
 static public GenerateResult Generate(
     string code,
     string?assemblyPath = null,
     IEnumerable <Assembly>?references       = null,
     CSharpParseOptions?parseOptions         = null,
     CSharpCompilationOptions?compileOptions = null,
     Func <string, Assembly>?loader          = null)
 {
     SyntaxTree[] trees = new[] { SyntaxFactory.ParseSyntaxTree(code, parseOptions) };
     if (String.IsNullOrEmpty(assemblyPath))
     {
         // Parsing is enough.
         return(new GenerateResult(trees));
     }
     using (var weakLoader = WeakAssemblyNameResolver.TemporaryInstall())
     {
         var collector = new HashSet <Assembly>();
         collector.Add(typeof(object).Assembly);
         if (references != null)
         {
             foreach (var a in references)
             {
                 if (collector.Add(a))
                 {
                     Discover(a, collector);
                 }
             }
         }
         return(Generate(compileOptions,
                         trees,
                         assemblyPath,
                         collector.Select(a => MetadataReference.CreateFromFile(new Uri(a.Location).LocalPath)),
                         loader)
                .WithLoadFailures(weakLoader.Conflicts));
     }
 }
Ejemplo n.º 5
0
 public object Resolve(Type t)
 {
     if (t == null)
     {
         throw new ArgumentNullException(nameof(t));
     }
     using (WeakAssemblyNameResolver.TemporaryInstall())
     {
         Context ctx = null;
         if (!TransientMode)
         {
             ctx = new Context(_container);
         }
         else
         {
             ctx = new Context(new SimpleServiceContainer(_container));
             foreach (var preLoad in _preLoadedTypes)
             {
                 Resolve(ctx, preLoad);
             }
         }
         return(Resolve(ctx, t));
     }
 }