简体   繁体   English

“组件”类型是在未引用的程序集中定义的。 您必须添加对程序集“System.ComponentModel.Primitives”的引用

[英]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.

相关问题 “System.Object”类型在未引用的程序集中定义。 您必须添加对程序集 .netstandard 的引用 - The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard 类型“ System.Data.Common.DbTransaction”在未引用的程序集中定义。 您必须添加对程序集的引用 - The type 'System.Data.Common.DbTransaction' is defined in an assembly that is not referenced. You must add a reference to assembly 为什么我得到:“IThirdParty”类型是在未引用的程序集中定义的。 您必须添加对程序集“ThirdPartyAssembly”的引用吗? - Why am I getting: The type 'IThirdParty' is defined in an assembly that is not referenced. You must add a reference to assembly 'ThirdPartyAssembly'? System.IObserver&#39;1 <T0> 在未引用的程序集中定义。 您必须添加对程序集的引用 - System.IObserver'1<T0> is defined in an assembly that is not referenced. You must add a reference to assembly “MarshalByRefObject”类型是在未引用的程序集中定义的。 您必须添加对程序集 &#39;mscorlib, Version=4.0.0.0 的引用 - The type 'MarshalByRefObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0 NSubstitute -3.x &#39;ValueType&#39; 在未引用的程序集中定义。 您必须添加对程序集“netstandard”的引用, - NSubstitute -3.x 'ValueType' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, 类型“ DbConnection”在未引用的程序集中定义。 我无法添加参考? - The type 'DbConnection' is defined in an assembly that is not referenced. I cannot add the reference? “IEnumerable &lt;&gt;”类型在未引用的程序集中定义。 System.Runtime - The type 'IEnumerable<>' is defined in an assembly that is not referenced. System.Runtime 类型“ IReportServerCredentials”在未引用的程序集中定义。 - type 'IReportServerCredentials' is defined in an assembly that is not referenced. 类型&#39;IAsyncOperationWithProgress &lt;,&gt;&#39;在未引用的程序集中定义。 - The type 'IAsyncOperationWithProgress<,>' is defined in an assembly that is not referenced.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM