gpt4 book ai didi

c# - 无法在 asp.net core 3.1 中添加对 CSharpCompilation 的 MetadataReference 的引用

转载 作者:行者123 更新时间:2023-12-04 12:58:31 32 4
gpt4 key购买 nike

我想创建运行时类,但是当使用 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,...}

关于c# - 无法在 asp.net core 3.1 中添加对 CSharpCompilation 的 MetadataReference 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62689490/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com