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 4 6996
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Chris |
last post by:
Hi
I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
|
by: jr |
last post by:
I am working in VC++ so this may be microsoft specific though I don't think
so. Excuse me if it is.
Assuming it isn't, can someone generally explain the purpose of having
both - is it purely a...
|
by: Eyeawanda Pondicherry |
last post by:
I have put some code together that creates an enum dynamically from some
database values.
The enum can be read perfectly by an application that references the
dynamically generated dll.
If I...
|
by: mflanagan |
last post by:
I have unmanaged C++ program that will load a managed C++ dll and then
call a function in that dll. The managed C++ routine will call some C#
routines. The unmanaged C++ main program will make...
|
by: RC |
last post by:
Hi,
I have a dll written by vb6.
AFunction(ByRef o as Variant)
I want to use it in csharp by System.Reflection.
Assembly asm = Assembly.LoadFrom("Lib.dll");
Type t =...
| |
by: Nancy Sui |
last post by:
I am trying to dynamically load a dll which specifies different rules
for evalution with the following code.
DataRow dr = ds.Tables.Rows;
string fileName = Convert.ToString(dr);
assemblyInstance...
|
by: =?Utf-8?B?TWF0dA==?= |
last post by:
I'm having a problem with a static class constructor being called twice. I
have the static class MasterTaskList which uses a BackgroundWorker to execute
multiple methods on a separate thread. The...
|
by: Smithers |
last post by:
I have a Windows Forms application that implements a plug-in architecture
whereby required assemblies are identified and loaded dynamically.
Here are the relevant classes:
A = application =...
|
by: Dave |
last post by:
I have a global.asax file with Application_Start defined and create some
static data there and in another module used in the asp.net application and
I realize that static data is shared amongst...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |