Ich versuche, eine Assembly dynamisch zu erstellen, mit Roslyn, und später von der ASP.NET CORE 'AddApplicationParts' Erweiterungsmethode in der Startphase meiner Webanwendung geladen werden .Verweise auf Roslyn kompiliert von Datei aus einem anderen Projekt
Ich lade eine CS-Datei extern von einem anderen Projekt, wo es gut funktioniert.
Hier ist der Code, ich habe alles zu arbeiten, aber ich kann nicht herausfinden, wie 'Referenzen' aus dem externen Projekt zu laden, habe ich versucht, die so genannten 'add references' aus metadatfile aber kein Glück.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Reflection;
using System.Collections.Immutable;
namespace WebApplication1
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
Pr2.Main2();
host.Run();
}
}
public class Pr2
{
public static void Main2()
{
string code = File.ReadAllText(@"c:\users\victor\documents\visual studio 2017\Projects\WebApplication2\WebApplication2\Controllers\HomeController.cs");
SyntaxTree tree = SyntaxFactory.ParseSyntaxTree(code);
CSharpCompilation compilation = CreateCompilation(tree);
SemanticModel model = compilation.GetSemanticModel(tree);
ShowLocalDeclarations(tree, model);
ShowDiagnostics(compilation);
ExecuteCode(compilation);
}
private static void ShowLocalDeclarations(SyntaxTree tree, SemanticModel model)
{
IEnumerable<LocalDeclarationStatementSyntax> locals = tree.GetRoot()
.DescendantNodes()
.OfType<LocalDeclarationStatementSyntax>();
foreach (var node in locals)
{
Microsoft.CodeAnalysis.TypeInfo type = model.GetTypeInfo(node.Declaration.Type);
Console.WriteLine("{0} {1}", type.Type, node.Declaration);
}
}
private static Assembly ExecuteCode(CSharpCompilation compilation)
{
Assembly roRet;
using (var stream = new MemoryStream())
{
compilation.Emit(stream);
roRet = Assembly.Load(stream.GetBuffer());
}
return roRet;
}
private static void ShowDiagnostics(CSharpCompilation compilation)
{
ImmutableArray<Diagnostic> diagnostics = compilation.GetDiagnostics();
foreach (var diagnostic in diagnostics)
{
// OVER HERE WE SEE THE ERRORS.
Console.WriteLine(diagnostic.ToString());
}
}
private static CSharpCompilation CreateCompilation(SyntaxTree tree)
{
CSharpCompilationOptions options = new CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary);
PortableExecutableReference reference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
CSharpCompilation compilation =
CSharpCompilation.Create("test")
.WithOptions(options)
.AddSyntaxTrees(tree)
.AddReferences(reference);
return compilation;
}
}
}
Die externe Datei in einem anderen asp.net Kernprojekt ist, gestartet von vs2017 Kernvorlagen frisch, den4.6.2 Framework. !!
using Microsoft.AspNetCore.Mvc;
namespace WebApplication2.Controllers
{
public class TestController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
return View();
}
public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";
return View();
}
public IActionResult Error()
{
return View();
}
}
}
Welche Fehler bekommen Sie? Sie müssen weitere Referenzen hinzufügen. – SLaks