对来自另一个项目的文件编译的roslyn的引用

时间:2017-03-29 19:58:07

标签: c# asp.net .net asp.net-core roslyn

我正在尝试使用roslyn动态构建程序集,以后再从中加载 ASP.NET CORE' AddApplicationParts'扩展方法,在我的网络应用程序的启动阶段。

我从另一个项目外部加载.cs文件,它工作得很好。

这是代码,我得到了一切工作,但我似乎无法弄清楚如何加载'引用'从外部项目,我尝试了所谓的“添加引用”#39;来自元数据文件,但没有运气。

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;
        }
    }
}

外部文件位于另一个asp.net核心项目中,使用 4.6.2框架从vs2017核心模板开始。 !!

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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题是您在编译文件时仅引用mscorlib,但您需要引用该文件所依赖的所有程序集。您可以尝试一次执行此一个程序集,但我认为更好的选择是利用您拥有第二个csproj的事实,其中包含您需要的所有信息。你只需要以某种方式从那里获取信息。

为此,您可以使用MSBuild。引用Microsoft.BuildMicrosoft.Build.Tasks.Core个包,然后使用此代码(改编自this answer):

private static IEnumerable<string> GetReferences(string projectFileName)
{
    var projectInstance = new ProjectInstance(projectFileName);
    var result = BuildManager.DefaultBuildManager.Build(
        new BuildParameters(),
        new BuildRequestData(projectInstance, new[]
        {
            "ResolveProjectReferences",
            "ResolveAssemblyReferences"
        }));

    IEnumerable<string> GetResultItems(string targetName)
    {
        var buildResult = result.ResultsByTarget[targetName];
        var buildResultItems = buildResult.Items;

        return buildResultItems.Select(item => item.ItemSpec);
    }

    return GetResultItems("ResolveProjectReferences")
        .Concat(GetResultItems("ResolveAssemblyReferences"));
}

// in Main2
var references = GetReferences(@"C:\code\tmp\roslyn references\WebApplication2\WebApplication2.csproj");

CSharpCompilation compilation = CreateCompilation(tree, references);

// in CreateCompilation
CSharpCompilation compilation =
    CSharpCompilation.Create("test")
        .WithOptions(options)
        .AddSyntaxTrees(tree)
        .AddReferences(references.Select(path => MetadataReference.CreateFromFile(path)));