簡體   English   中英

無法在 asp.net 核心 3.1 中添加對 CSharpCompilation 的 MetadataReference 的引用

[英]Not able to add reference to MetadataReference of CSharpCompilation in asp.net core 3.1

我想創建運行時 class 但是當使用來自 Stimulsoft.Report.Dictionary 的屬性 [StiAlias("id")]; 發送錯誤:

[0] = (1,101):錯誤 CS0012:“屬性”類型是在未引用的程序集中定義的。 您必須添加對程序集“netstandard,Version=2.1.0.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51”的引用。

public object CreateClassRunTime()
    {
        string strClass = 
            @"using System; " +
        //"using System.Collections.Generic;" +
        "using Stimulsoft.Report.Dictionary;" +

              "namespace VModel { public class AddressViewTest { " +
          "[StiAlias(\"id\")]" +
          "public int id { get; set; }" +
        
         " public int? updUser { get; set; } } }";


        SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(strClass);
        string assemblyName = Path.GetRandomFileName();
        var refPaths = new[] {
            typeof(object).Assembly.Location,
            typeof(System.ComponentModel.DataAnnotations.DisplayAttribute).Assembly.Location,
            typeof(System.Runtime.AssemblyTargetedPatchBandAttribute).Assembly.Location,
            typeof(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo).Assembly.Location,
            typeof(Stimulsoft.Report.Dictionary.StiAliasAttribute).GetTypeInfo().Assembly.Location,
           
        };
        MetadataReference[] references = refPaths.Select(r => MetadataReference.CreateFromFile(r)).ToArray();

        
        CSharpCompilation compilation = CSharpCompilation.Create(
            assemblyName,
            syntaxTrees: new[] { syntaxTree },
            references: references,
            options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
        object instance = null;
        try
        {
            byte[] image = null;
            using (var ms = new MemoryStream())
            {
                EmitResult result = compilation.Emit(ms);

                if (!result.Success)
                {
                    Console.Write(result.Diagnostics.First().GetMessage());
                }
                image = ms.ToArray();
                //Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(ms);
                
                //instance = assembly.CreateInstance("VModel.AddressViewTest");
            }
            Assembly assembly = null;

            
            using (var stream = new MemoryStream(image))
                assembly = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromStream(stream);
            //var type = assembly.GetType("VModel.AddressViewTest");
            instance = assembly.CreateInstance("VModel.AddressViewTest");
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }

        return instance;
    }

由廣告解決錯誤:

var ns = Assembly.Load("netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51");

和廣告參考:

var refPaths = new[] {
            ns.Location,
            typeof(object).Assembly.Location,...

}

通過添加解決了錯誤:

var ns = Assembly.Load("netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51");

和廣告參考:

var refPaths = new[] {
        ns.Location,
        typeof(object).Assembly.Location,...}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM