How to import C# dll dynamically
-
Hi, I've created a C# dll using the Class library project and have a method in there that just returns a string. I'd like to import this dll in my code dynamically instead of doing the "Add reference". How would I do that? Any help would be greatly appreciated.:confused:
-
Hi, I've created a C# dll using the Class library project and have a method in there that just returns a string. I'd like to import this dll in my code dynamically instead of doing the "Add reference". How would I do that? Any help would be greatly appreciated.:confused:
I personally have not tried this, but this might point you in the right direction: Try assem = System.Reflection.Assembly.Load(...) to load your assembly (C# dll). Then t = assem.GetType(...) to get your type (class, etc.) Then use t.InvokeMember(...) to call a method You can also get a ConstructorInfo object through the Type object and create an instance of your class through ConstructorInfo.Invoke(...) -- Peter Stephens
-
Hi, I've created a C# dll using the Class library project and have a method in there that just returns a string. I'd like to import this dll in my code dynamically instead of doing the "Add reference". How would I do that? Any help would be greatly appreciated.:confused:
First you use
Assembly myAssembly = Assembly.LoadFrom(myDllPath);
passing in the location of your dll. Next you use the returned Assembly and search it for the Type you want to use (Type is the overlying type for all classes, enums, structs, delegates, and events).Type myType = myAssembly.GetType("myNamespace.myType");
. Now with your type you can create an instance of it.object myInstance = Activator.CreateInstance(myType);
Now comes the fun part, invoking your method!string retVal = (string) myType.InvokeMember("MyMethodName", BindingFlags.Public | BindingFlags.InvokeMethod, null, myInstance, new object { /* any parameters here, leave empty if there are none */ } );
There you have it! You've not successfully run a method and retreived the return value. To sum it up again,Assembly myAssembly = Assembly.LoadFrom(/* Assembly filename/path */ myDllFilename);
Type myType = myAssembly.GetType(/* The type to load, with namespace */ myTypeName);
object myInstance = Activator.CreateInstance(/* The Type for the type to create */ myType);
string retVal = (string) myType.InvokeMember(
/* Method name */ "myMethodName",
/* BindingFlags, tells what to look for */ BindingFlags.Public | BindingFlags.InvokeMethod,
/* The binder to use, null to use the defaul */ null,
/* The instance of the type to execute the method on */ myInstance,
/* The parameters to the method */ new object { /* Leave empty if no parameters */ }
);HTH, James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
-
I personally have not tried this, but this might point you in the right direction: Try assem = System.Reflection.Assembly.Load(...) to load your assembly (C# dll). Then t = assem.GetType(...) to get your type (class, etc.) Then use t.InvokeMember(...) to call a method You can also get a ConstructorInfo object through the Type object and create an instance of your class through ConstructorInfo.Invoke(...) -- Peter Stephens
*sigh* I should have reloaded the forum before I started typing my post :-P James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
-
*sigh* I should have reloaded the forum before I started typing my post :-P James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
Yes, but yours has better examples :-D -- Peter Stephens
-
Hi, I've created a C# dll using the Class library project and have a method in there that just returns a string. I'd like to import this dll in my code dynamically instead of doing the "Add reference". How would I do that? Any help would be greatly appreciated.:confused: