Home > database >  The type 'Component' is defined in an assembly that is not referenced. You must add a refe
The type 'Component' is defined in an assembly that is not referenced. You must add a refe

Time:08-20

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'.

CodePudding user response:

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));

CodePudding user response:

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));
  • Related