By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,564 Members | 1,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,564 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,

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

Greetings,
Bram

"Jonathan Roewen" <co*************@yahoo.co.nz> wrote in message
news:F2**********************************@microsof t.com...
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

Nov 15 '05 #2

P: n/a
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).
Nov 15 '05 #3

P: n/a
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

Nov 15 '05 #4

P: n/a
Jonathan Roewen <co*************@yahoo.co.nz> wrote:
I've pasted in some code to demonstrate exactly what I'm trying to
do, and what goes wrong.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.