0

I have library TestProject1. Namespace - TestProject1. here i have class - Class1. Can i load it from another project using full class name (TestProject1.Class1)? (dll is in another project's folder). I want to see solution without going throw all "*.dll" files in folder.

Only dynamic load.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Vitaly Senko
  • 323
  • 2
  • 13
  • Check this post: http://stackoverflow.com/questions/465488/can-i-load-a-net-assembly-at-runtime-and-instantiate-a-type-knowing-only-the-na it includes dynamic loading and type activation/constructing. – Stefan May 15 '14 at 11:41

2 Answers2

2

Yes, since you have the reference of this another assembly in your project, you can create a Assembly object from a type and get some properties of this type, for sample:

Assembly asm = Assembly.GetAssembly(typeof(Class1));

string fullName = asm.FullName;

If you need to load a assembly that is not referenced into your project, you can use the Assembly.LoadFile method, passing the path of dll, for sample:

var asm = System.Reflection.Assembly.LoadFile("C:\\project.dll");

// Get the type to use.
Type class1 = asm.GetType("Class1");

// Get the method to call.
MethodInfo myMethod = class1.GetMethod("MethodA");

// Create an instance. 
object obj = Activator.CreateInstance(class1);

And work with asm assembly object, you will not have a strongly typed code, but you can use Reflection to use the types from this assembly.

Remember to include the reflection namespace:

using System.Reflection;
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
0

Ehm, my own answer is to use

Assembly.LoadWithPatrialName("TestProject1.Class1");

Am i right?

Vitaly Senko
  • 323
  • 2
  • 13