Hi,
I have a DLL file,
it has a class,
and a delegate declared at namespace level, (the definition of this
delegate
is not in the above class)
I need to load this DLL file,
create an instance of that delegate,
then pass it to a function of that class.
I can do it in .NET framework using Delegate.CreateInstacne, but I don't
know how to do it in .NET CF.
Sample code is as follows, they work in .NET Framework:
public void GameOverFn(string reason)
{
MessageBox.Show(reason);
}
object creator;
object HANDLER;
private void button1_Click(object sender, System.EventArgs e)
{
foreach( string fileName in Directory.GetFiles("\\Program
Files\\App1\\",
"*.dll") )
{
try
{
Assembly a = Assembly.LoadFrom(fileName);
foreach( Type t in a.GetTypes() )
{
if (t.FullName.Equals("GameServerNS.GameOverHandler"))
{
HANDLER = Delegate.CreateDelegate(t, this,
"GameOverFn");
}
if( t.IsClass && t.FullName.EndsWith("Main"))
{
creator = a.CreateInstance(t.FullName);
try
{
MethodInfo StartServer = t.GetMethod("StartServer");
Object[] args1 = {HANDLER};
string status = (string)
StartServer.Invoke(creator,args1);
}
catch (NullReferenceException exc)
{
MessageBox.Show(exc.ToString());
}
catch(Exception e2)
{
MessageBox.Show(e2.ToString());
}
}
}
}
catch( FileLoadException exc)
{
// deal with the fact that some of the dlls may not be assemblies
}
}
}
These codes may have other problems, please leave them, thy are just for
my
testing.
Thanks much!
KC Eric
--
-> ????,????,?????????FYP??
-> ??????????,??????????,?????????
-> ??????????? XDDD
-> To be energetic to strive for the best. To build our FYP idealism in a
flourishing, rapid, perfect and efficient manner.
-> To firmly adhere to Pango's Four Statements. To comprehensively
implement
The Giant Pango's Great Scheme. To stride forward to the Initialization of
The Four Objects.
-> Parents are not as close as Pango. XDDD
Alex Feinman said:
What are you trying to do?
--
Alex Feinman
---
Visit
http://www.opennetcf.org
KC Eric said:
Hi all,
How to achieve Delegate.CreateDelegate() in .NET CF?
Thanks!
KC Eric
--
-> ¹ª¨¬·F«l¡A¤Oª§¤W´å¡A¦h§Ö¦n¬Ù¦a«Ø³]²z·QFYP¥D¸q
-> °í¨M¿í±qÄPô¥|¶µªíz¡A¥þ±¸¨¹ê¤jÄPª÷Á`¸ô½u¡A¨«¦V¥|Óª«¥óªì©l¤Æ
-> ¯R¿Ë®Q¿Ë³£¤£¤Îª÷ÄPô¿Ë XDDD
-> To be energetic to strive for the best. To build our FYP idealism in a
flourishing, rapid, perfect and efficient manner.
-> To firmly adhere to Pango's Four Statements. To comprehensively
implement
The Giant Pango's Great Scheme. To stride forward to the Initialization of
The Four Objects.
-> Parents are not as close as Pango. XDDD