2

I compiled a c# code dynamically in a .Net Core 3.1 project. I saved the result as a .dll file and trying to use it in another project. In destination project when I add it as a reference and trying to use, I get this error

the type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Private.CoreLib, Version=4.0.0.0.

I tried to compile the code as netstandard 2.1. And also there is reference to System.Private.CoreLib during the compilation in _references but the version is newer than 4.0.0.0.

_references = new List<MetadataReference>();
_references.Add(MetadataReference.CreateFromFile(typeof(object).Assembly.Location));
_references.Add(MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.1").Location));
 _references.Add(MetadataReference.CreateFromFile(typeof(Newtonsoft.Json.JsonConvert).GetTypeInfo().Assembly.Location));

How can I solve this issue?

0

1 Answer 1

1

You need a reference to the netstandard library, which doesn't technically define any types, to match your typeof(object).

    var netstandard = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == "netstandard").Single()
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. But I have it in my references _references.Add(MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.1").Location));
You said 3.1 in the question, so your typeof(object).Assembly would be incompatible.
I said .Net Core 3.1 and the related .Net Standard is 2.1
Still, since you are implicitly referring to the typeof(object).Assembly that is loaded in your process. It makes sense that you should also refer to the compatible netstandard Assembly that you are also currently using. Avoiding any magic numbers that may be incorrect.
You are right. I tried that without version number and still get same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.