简体   繁体   中英

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. The System.ComponentModel.Primitives package could not added to my 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'.

As the error indicates, you need to add the reference of System.ComponentModel.Primitives

You could try: references.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.ComponentModel.Primitives")).Location));

I could not install System.ComponentModel.Primitives package.

but after installing the following package:

Microsoft.CodeAnalysis, Microsoft.CodeAnalysis Microsoft.CodeAnalysis.Common, Microsoft.CodeAnalysis.CSharp,

the installation of System.ComponentModel.Primitives is done without error.

and I add the following code to my code:

references.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.ComponentModel.Primitives")).Location));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard The type 'System.Data.Common.DbTransaction' is defined in an assembly that is not referenced. You must add a reference to assembly 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'1<T0> is defined in an assembly that is not referenced. You must add a reference to assembly 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 'ValueType' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, The type 'DbConnection' is defined in an assembly that is not referenced. I cannot add the reference? The type 'IEnumerable<>' is defined in an assembly that is not referenced. System.Runtime type 'IReportServerCredentials' is defined in an assembly that is not referenced. The type 'IAsyncOperationWithProgress<,>' is defined in an assembly that is not referenced.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM