Delegate.CreateDelegate()

K

KC Eric

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
 
K

KC Eric

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
 
A

Alex Feinman [MVP]

I see. I think you are approaching this from a wrong side. If you want to
implement plugin functionality, define an interface in a common module (or,
better yet a base, perhaps abstract class). Define an event in it. When you
load a plugin, create an instance of the object ( you can actually enumerate
all types unitl you find one derived from your base class), cast it to the
base class and attach an event handler to it in a regular way. Both your
main project and the plugin need to reference a certain common dll where yor
base class is defined.

--
Alex Feinman
---
Visit http://www.opennetcf.org
KC Eric said:
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
 
K

KC Eric

Hi Alex,

Thanks so much for your reply! I will try it out.
Is there any existing sample which I can reference to?

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:
I see. I think you are approaching this from a wrong side. If you want to
implement plugin functionality, define an interface in a common module (or,
better yet a base, perhaps abstract class). Define an event in it. When you
load a plugin, create an instance of the object ( you can actually enumerate
all types unitl you find one derived from your base class), cast it to the
base class and attach an event handler to it in a regular way. Both your
main project and the plugin need to reference a certain common dll where yor
base class is defined.

--
Alex Feinman
---
Visit http://www.opennetcf.org
KC Eric said:
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
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
 
K

KC Eric

Sergey,

Thanks so much for your info!

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top