Calling a static function in a dynamically loaded assembly (DLL) | | |
Hi
I've got loading assemblies dynamically done (wasn't too difficult). Now I want to lookup a static function in the loaded assembly, and if found, return it somehow, and call it from my app. So far, my efforts have failed miserably
My function takes custom objects as parameters, along the lines of: string MyFunction(XmlNode node, MyNamespace.MyObject myObject, String str) { ...
I've tried using delegates, and also MethodInfo, but neither seem to like me at present
I figured I could write a static helper function in my DLL which then returns a delegate wrapping the found static method in the DLL. I then used MethodInfo to find the helper function, and call it using Invoke(). Whilst the Invoke call itself succeeds, and returns the delegate, I can't cast it to my delegate type .. it always throws an exception
Regards
Jonathan Roewen | | | | re: Calling a static function in a dynamically loaded assembly (DLL)
Hi,
can't you just call the Invoke method on the MethodInfo of the static
function ?
Greetings,
Bram
"Jonathan Roewen" <consulatewizard@yahoo.co.nz> wrote in message
news:F2D69149-C847-4CCA-B7FF-625A4AA84A45@microsoft.com...[color=blue]
> Hi,
>
> I've got loading assemblies dynamically done (wasn't too difficult). Now I[/color]
want to lookup a static function in the loaded assembly, and if found,
return it somehow, and call it from my app. So far, my efforts have failed
miserably.[color=blue]
>
> My function takes custom objects as parameters, along the lines of: string[/color]
MyFunction(XmlNode node, MyNamespace.MyObject myObject, String str) { ... }[color=blue]
>
> I've tried using delegates, and also MethodInfo, but neither seem to like[/color]
me at present.[color=blue]
>
> I figured I could write a static helper function in my DLL which then[/color]
returns a delegate wrapping the found static method in the DLL. I then used
MethodInfo to find the helper function, and call it using Invoke(). Whilst
the Invoke call itself succeeds, and returns the delegate, I can't cast it
to my delegate type .. it always throws an exception.[color=blue]
>
> Regards,
>
> Jonathan Roewen[/color] | | | | re: Calling a static function in a dynamically loaded assembly (DLL)
I've tried using MethodInfo.Invoke, as I mentioned briefly.
But then the problem is that I can't cast the returned object to my delegate type.
I also tried rewriting it so that the method to return a delegate returns a MethodInfo object instead.
However, the end result is either that I can't create a delegate, or I can't invoke the method (an exception is always thrown). | | | | re: Calling a static function in a dynamically loaded assembly (DLL)
I've pasted in some code to demonstrate exactly what I'm trying to do, and what goes wrong
//-- myexe.cs -> myexe.ex
namespace MyNamespac
public delegate string MyDelegate(XmlNode node)
public delegate MyDelegate MyGetterDelegate(string name)
public class MyClas
static void Main(string[] args
LoadLibrary("mydll.dll")
tr
Lookup1("test")
catch ( Exception ex1
Console.WriteLine(ex1)
tr
Lookup2("test")
catch ( Exception ex2
Console.WriteLine(ex2)
private static ArrayList MyArrayList = new ArrayList()
public interface IMyInterfac
MyDelegate GetMyDelegate(string name)
public static void LoadLibrary(string filename
if ( !filename.EndsWith(".dll") ) return
Assembly assembly = Assembly.LoadFrom(filename)
foreach ( Type type in assembly.GetTypes()
if ( type.GetInterface("IMyInterface") != null
Console.WriteLine("[status] added {0} from {1}", type, filename)
object obj = type.GetConstructor(Type.EmptyTypes).Invoke(null)
//-- REFER NOTE ON
Console.WriteLine("Types implemented by {0}...", type)
foreach ( Type t in obj.GetType().GetInterfaces()
Console.WriteLine("{0}, {1}", t, t.IsInstanceOfType(obj))
Console.WriteLine("Test castability...")
Type itype = typeof(IMyInterface)
Console.WriteLine("{0}, {1}", itype, itype.IsIntanceOfType(obj))
MyArrayList.Add(obj)
public static MyDelegate Lookup1(string name
//-- REFER NOTE TW
foreach ( IMyInterface imi in MyArrayList
MyDelegate md = imi.GetMyDelegate(name)
if ( md != null ) return md
return null
public static MyDelegate Lookup2(string name
Type mgdType = typeof(MyGetterDelegate)
foreach ( object lib in MyArrayList
//-- REFER NOTE THRE
MyGetterDelegate mgd
(MyGetterDelegate)Delegate.CreateDelegate(mgdType, lib, "GetMyDelegate")
MyDelegate md = mgd(name)
if ( md != null ) return md
return null
//-- mydll.cs -> mydll.dl
namespace MyDllNamespac
public class MyDllClass : MyNamespace.MyClass.IMyInterfac
public MyNamespace.MyDelegate GetMyDelegate(string name)
if ( name == "test" ) return new MyNamespace.MyDelegate(TestFunction)
else return null
public static string TestFunction(XmlNode node
return node.InnerXml
NOTE ONE
Generates output along the lines of
Types implemented by MyDllNamespace.MyDllClass..
MyNamespace.MyClass+IMyInterface, Tru
Test castability..
MyNamespace.MyClass+IMyInterface, Fals
NOTE TWO
Cast fails (as note one indicates
Exception: System.InvalidCastException: Specified cast is not valid
NOTE THREE
Creating delegate fail
Exception: System.ArgumentException: Error binding to target method | | | | re: Calling a static function in a dynamically loaded assembly (DLL)
Jonathan Roewen <consulatewizard@yahoo.co.nz> wrote:[color=blue]
> I've pasted in some code to demonstrate exactly what I'm trying to
> do, and what goes wrong.[/color]
I suspect your problem is in declaring the interface and delegate in
the DLL as well as the EXE. See http://www.pobox.com/~skeet/csharp/plugin.html for more information and
a solution.
--
Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|