[英]The type 'Component' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.ComponentModel.Primitives'
I am using Microsoft.CodeAnalysis.CSharp
package in my ASP.NET Core project.我在我的 ASP.NET 核心项目中使用
Microsoft.CodeAnalysis.CSharp
package。 The System.ComponentModel.Primitives
package could not added to my package. System.ComponentModel.Primitives
package 无法添加到我的 package。
Here is my code:这是我的代码:
List<MetadataReference> references = new List<MetadataReference>();
references.Add(MetadataReference.CreateFromFile(typeof(object).Assembly.Location));
references.Add(MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location));
references.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Console")).Location));
references.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime")).Location));
references.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Data.SqlClient")).Location));
references.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Data")).Location));
references.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Data.Common")).Location));
references.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.ComponentModel.DataAnnotations")).Location));
var code = @"
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine(1);
}
}
private DataTable ExecuteQuery(string query)
{
{
query = query.Replace(""'"", ""''"");
DataTable dataTable = new DataTable();
using (var con = new System.Data.SqlClient.SqlConnection(_connectionString))
{
{
using var sqlCommand = con.CreateCommand();
sqlCommand.CommandType = CommandType.Text;
sqlCommand.CommandText = query;
con.Open();
System.Data.SqlClient.SqlDataAdapter sqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCommand);
sqlDataAdapter.Fill(dataTable);
con.Close();
}
}
return dataTable;
}
}
";
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
string assemblyName = Path.GetRandomFileName();
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
syntaxTrees: new[] { syntaxTree },
references: references,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,
usings: new[] {
"System",
"System.IO",
"System.Linq",
"System.Collections.Generic" })
);
using (var ms = new MemoryStream())
{
EmitResult result = compilation.Emit(ms);
if (result.Success)
{
ms.Seek(0, SeekOrigin.Begin);
Assembly assembly = Assembly.Load(ms.ToArray());
Type type = assembly.GetType("RoslynCompileSample.MainClass");
object obj = Activator.CreateInstance(type);
var MethodOutput = type.InvokeMember("Main",
BindingFlags.Default | BindingFlags.InvokeMethod,
null,
obj,
null);
}
}
And here is the errors:这是错误:
The type 'Component' is defined in an assembly that is not referenced.
“组件”类型是在未引用的程序集中定义的。 You must add a reference to assembly 'System.ComponentModel.Primitives, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
您必须添加对程序集“System.ComponentModel.Primitives, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”的引用。
As the error indicates, you need to add the reference of System.ComponentModel.Primitives
如错误所示,您需要添加
System.ComponentModel.Primitives
的引用
You could try: references.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.ComponentModel.Primitives")).Location));
您可以尝试:
references.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.ComponentModel.Primitives")).Location));
I could not install System.ComponentModel.Primitives
package.我无法安装
System.ComponentModel.Primitives
package。
but after installing the following package:但是在安装了以下 package 之后:
Microsoft.CodeAnalysis, Microsoft.CodeAnalysis Microsoft.CodeAnalysis.Common, Microsoft.CodeAnalysis.CSharp,
Microsoft.CodeAnalysis、Microsoft.CodeAnalysis Microsoft.CodeAnalysis.Common、Microsoft.CodeAnalysis.CSharp、
the installation of System.ComponentModel.Primitives
is done without error. System.ComponentModel.Primitives
的安装没有错误。
and I add the following code to my code:我将以下代码添加到我的代码中:
references.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.ComponentModel.Primitives")).Location));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.