pass delegate to a function in a dll file

  • Thread starter Thread starter KC Eric
  • Start date Start date
K

KC Eric

Hi all,

I have a dll file, it has a class, say:

class Temp,

this class has a function which has a delegate as a parameter,
say:

public void Test(GameOverHandler _overHandler)

the delegate type is declared in Namespace level,
say:
public delegate void GameOverHandler(string reason);

Now I have Another project, which needs to use that class and function in
That DLL,
I know that if everthing is in the same project, I can pass a function, say
public Abc(string reason), to Test easily by:
Test(new GameOverHandler(Abc));

How do I do this if that class and the delegate is defined in a dll file?

I have part of my code here,


foreach( string fileName in
Directory.GetFiles(Environment.CurrentDirectory.ToString(), "*.dll") )
{
try
{
Assembly a = Assembly.LoadFrom(fileName);
foreach( Type t in a.GetTypes() )
{
if (t.FullName.Equals("GameServerNS.GameOverHandler"))
{
// Here I want to instantiate the delegate, something like:
// GameOverHandler HANDLER = new GameOverHandler(abc);
// but I don't know how
}

if( t.IsClass && t.FullName.Equals("Game"))
{
object creator = a.CreateInstance(t.FullName);
try
{
MethodInfo StartServer = t.GetMethod("Test");
Object[] args1 = { //here I want to pass the Abc or the HANDLER,
but I also don't know how };
string status = (string) StartServer.Invoke(creator,args1);
catch (NullReferenceException exc)
{
}


}
}
}


I know the above code have other problems, but please leave them, I just use
the above code for my testing.

Thanks much!

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
 
Hi!
if (t.FullName.Equals("GameServerNS.GameOverHandler"))
{
// Here I want to instantiate the delegate, something like:
// GameOverHandler HANDLER = new GameOverHandler(abc);
// but I don't know how
}

Try this:

// We assume that the "abc" method is defined on the same class the code
below belongs to AND
// that the "abc" method is not static.
GameOverHandler HANDLER = (GameOverHandler)Activator.CreateInstance(t, new
object[] {this, "abc"});

MethodInfo StartServer = t.GetMethod("Test");
Object[] args1 = { //here I want to pass the Abc or the HANDLER,
but I also don't know how };
string status = (string) StartServer.Invoke(creator,args1);

Try this:

object[] args1 = new object[] {HANDLER};
string status = (string) StartServer.Invoke(creator,args1);

--
Sincerely,
Dmytro Lapshyn

KC Eric said:
Hi all,

I have a dll file, it has a class, say:

class Temp,

this class has a function which has a delegate as a parameter,
say:

public void Test(GameOverHandler _overHandler)

the delegate type is declared in Namespace level,
say:
public delegate void GameOverHandler(string reason);

Now I have Another project, which needs to use that class and function in
That DLL,
I know that if everthing is in the same project, I can pass a function,
say
public Abc(string reason), to Test easily by:
Test(new GameOverHandler(Abc));

How do I do this if that class and the delegate is defined in a dll file?

I have part of my code here,


foreach( string fileName in
Directory.GetFiles(Environment.CurrentDirectory.ToString(), "*.dll") )
{
try
{
Assembly a = Assembly.LoadFrom(fileName);
foreach( Type t in a.GetTypes() )
{
if (t.FullName.Equals("GameServerNS.GameOverHandler"))
{
// Here I want to instantiate the delegate, something like:
// GameOverHandler HANDLER = new GameOverHandler(abc);
// but I don't know how
}

if( t.IsClass && t.FullName.Equals("Game"))
{
object creator = a.CreateInstance(t.FullName);
try
{
MethodInfo StartServer = t.GetMethod("Test");
Object[] args1 = { //here I want to pass the Abc or the HANDLER,
but I also don't know how };
string status = (string) StartServer.Invoke(creator,args1);
catch (NullReferenceException exc)
{
}


}
}
}


I know the above code have other problems, but please leave them, I just
use
the above code for my testing.

Thanks much!

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
 
Thanks much!
but for:
GameOverHandler HANDLER = (GameOverHandler)Activator.CreateInstance(t, new
object[] {this, "abc"});
it throws a System.MissingMethodException,

For delegate, to instantiate it, we do something like GameOverHandler
HANDLER = new GameOverHandler(abc),
but I really have no idea on how to do it using CreateInstance.
any ideas?

Thanks!

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

Dmytro Lapshyn said:
Hi!
if (t.FullName.Equals("GameServerNS.GameOverHandler"))
{
// Here I want to instantiate the delegate, something like:
// GameOverHandler HANDLER = new GameOverHandler(abc);
// but I don't know how
}

Try this:

// We assume that the "abc" method is defined on the same class the code
below belongs to AND
// that the "abc" method is not static.
GameOverHandler HANDLER = (GameOverHandler)Activator.CreateInstance(t, new
object[] {this, "abc"});

MethodInfo StartServer = t.GetMethod("Test");
Object[] args1 = { //here I want to pass the Abc or the HANDLER,
but I also don't know how };
string status = (string) StartServer.Invoke(creator,args1);

Try this:

object[] args1 = new object[] {HANDLER};
string status = (string) StartServer.Invoke(creator,args1);

--
Sincerely,
Dmytro Lapshyn

KC Eric said:
Hi all,

I have a dll file, it has a class, say:

class Temp,

this class has a function which has a delegate as a parameter,
say:

public void Test(GameOverHandler _overHandler)

the delegate type is declared in Namespace level,
say:
public delegate void GameOverHandler(string reason);

Now I have Another project, which needs to use that class and function in
That DLL,
I know that if everthing is in the same project, I can pass a function,
say
public Abc(string reason), to Test easily by:
Test(new GameOverHandler(Abc));

How do I do this if that class and the delegate is defined in a dll file?

I have part of my code here,


foreach( string fileName in
Directory.GetFiles(Environment.CurrentDirectory.ToString(), "*.dll") )
{
try
{
Assembly a = Assembly.LoadFrom(fileName);
foreach( Type t in a.GetTypes() )
{
if (t.FullName.Equals("GameServerNS.GameOverHandler"))
{
// Here I want to instantiate the delegate, something like:
// GameOverHandler HANDLER = new GameOverHandler(abc);
// but I don't know how
}

if( t.IsClass && t.FullName.Equals("Game"))
{
object creator = a.CreateInstance(t.FullName);
try
{
MethodInfo StartServer = t.GetMethod("Test");
Object[] args1 = { //here I want to pass the Abc or the HANDLER,
but I also don't know how };
string status = (string) StartServer.Invoke(creator,args1);
catch (NullReferenceException exc)
{
}


}
}
}


I know the above code have other problems, but please leave them, I just
use
the above code for my testing.

Thanks much!

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
 
Ah, looks like there's a better way:

GameOverHandler HANDLER = (GameOverHandler)Delegate.CreateDelegate(t, this,
"abc");

--
Sincerely,
Dmytro Lapshyn

KC Eric said:
Thanks much!
but for:
GameOverHandler HANDLER = (GameOverHandler)Activator.CreateInstance(t, new
object[] {this, "abc"});
it throws a System.MissingMethodException,

For delegate, to instantiate it, we do something like GameOverHandler
HANDLER = new GameOverHandler(abc),
but I really have no idea on how to do it using CreateInstance.
any ideas?

Thanks!

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

Dmytro Lapshyn said:
Hi!
if (t.FullName.Equals("GameServerNS.GameOverHandler"))
{
// Here I want to instantiate the delegate, something like:
// GameOverHandler HANDLER = new GameOverHandler(abc);
// but I don't know how
}

Try this:

// We assume that the "abc" method is defined on the same class the code
below belongs to AND
// that the "abc" method is not static.
GameOverHandler HANDLER = (GameOverHandler)Activator.CreateInstance(t,
new
object[] {this, "abc"});

MethodInfo StartServer = t.GetMethod("Test");
Object[] args1 = { //here I want to pass the Abc or the HANDLER,
but I also don't know how };
string status = (string) StartServer.Invoke(creator,args1);

Try this:

object[] args1 = new object[] {HANDLER};
string status = (string) StartServer.Invoke(creator,args1);

--
Sincerely,
Dmytro Lapshyn

KC Eric said:
Hi all,

I have a dll file, it has a class, say:

class Temp,

this class has a function which has a delegate as a parameter,
say:

public void Test(GameOverHandler _overHandler)

the delegate type is declared in Namespace level,
say:
public delegate void GameOverHandler(string reason);

Now I have Another project, which needs to use that class and function in
That DLL,
I know that if everthing is in the same project, I can pass a function,
say
public Abc(string reason), to Test easily by:
Test(new GameOverHandler(Abc));

How do I do this if that class and the delegate is defined in a dll file?

I have part of my code here,


foreach( string fileName in
Directory.GetFiles(Environment.CurrentDirectory.ToString(), "*.dll") )
{
try
{
Assembly a = Assembly.LoadFrom(fileName);
foreach( Type t in a.GetTypes() )
{
if (t.FullName.Equals("GameServerNS.GameOverHandler"))
{
// Here I want to instantiate the delegate, something like:
// GameOverHandler HANDLER = new GameOverHandler(abc);
// but I don't know how
}

if( t.IsClass && t.FullName.Equals("Game"))
{
object creator = a.CreateInstance(t.FullName);
try
{
MethodInfo StartServer = t.GetMethod("Test");
Object[] args1 = { //here I want to pass the Abc or the HANDLER,
but I also don't know how };
string status = (string) StartServer.Invoke(creator,args1);
catch (NullReferenceException exc)
{
}


}
}
}


I know the above code have other problems, but please leave them, I
just
use
the above code for my testing.

Thanks much!

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

Thanks for your input! But it still doesn't work,
and now I solved it, here's my solution and it works.

In my another project, I declared a member:
object handler;

then when the GameOverHandler is found in the DLL, I do this:
handler = Delegate.CreateDelegate(t, this, "abc"); // note that I didn't
cast it

then for the class in the DLL:
MethodInfo StartServer = t.GetMethod("Test");
Object[] args1 = {handler};
string status = (string) StartServer.Invoke(creator,args1);


I am very happy that I solve this delegate problem finally, thanks for your
input!

KC Eric
 
Back
Top