1

How do you do this in .Net Standard 2.1

var instance = Activator.CreateInstance("SomeAssemblyName", "SomeClass");
instance.Unwrap()

1 Answer 1

2

I used to create the assemblies in .netCore using the below code,

var myAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(@"Directory_Path");
var myType = myAssembly.GetType("Class_Name");
var myInstance = Activator.CreateInstance(myType);

In .Net Standard also, it should work.

Edit: Try this for .NetStandard,

var assembly = Assembly.LoadFrom("directoryPath");
var type = assembly.GetType("ClassName");

var instance = Activator.CreateInstance(type);
Sign up to request clarification or add additional context in comments.

12 Comments

I am getting this error when i run the application "Could not load file or assembly 'System.Runtime.Loader, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified."
Is the System.Runtime.Loader assembly referred in your application? If yes, please check if multiple versions are installed in different projects and correct them
Run this, "%userprofile%\.nuget\packages\System.Runtime.Loader" This should let you know the locally installed versions
It will show whatever installed in your system. If you want to check in your solution, you need to use the solution explorer in VS and search it
Thanx a ton badri. But i just did a workaround by passing the Assembly object itself to the .Net standard library project. There is one more question i have posted which is for How to use C# code to list all Namespaces in the .Net Standard library from within the .Net standard project itself. Can you see that. It is here stackoverflow.com/questions/59026604/…
|

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.