Calling a static function in a dynamically loaded assembly (DLL)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Hi,

can't you just call the Invoke method on the MethodInfo of the static
function ?

Greetings,
Bram

Jonathan Roewen said:
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.
 
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).
 
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
 
Back
Top