void () type to object

M

Max

When you add an event handler to some object you need to specify the function to call when the event occurs. Suppose there is a
class TesterClosed() which is called whenever the Tester is closed. Code below:

public void TesterClosed(){}

Tester m_tester = new Tester();
m_tester.Close += new Tester.CloseEventHandler(TesterClosed);

Now suppose I want to do this with late binding where I need to work with objects. The parameter TesterClosed in
Tester.CloseEventHandler(....) is of type "void () type". How do I convert this into an object that I can pass to the same function
with late binding? Code below which I'm not sure I'm doing right:

//m_tester.Close += new Tester.CloseEventHandler(TesterClosed);

Type objClassType = Type.GetTypeFromProgID("Tester.CloseEventHandler");
object [] objParameters_Late = new object[1];
objParameters_Late[0] = TesterClosed; /*I get an error here*/
object objTesterEvent_Late = Activator.CreateInstance(objClassType, objParameters_Late);

objParameters_Late = new object[1];
objParameters_Late[0] = objTesterEvent_Late;
m_tester_Late.GetType().InvokeMember("Close", BindingFlags.InvokeMethod, null, m_tester_Late, objParameters_Late);
 
M

Max

Abubakar, I think you miss read my question. You see, I can certainly do that when I know that Tester exists and this will be true
if the library corresponding to Tester is imported. Now suppose there are different versions of the Tester library and you want your
code to work with all of them. A better example is if you can think of Tester as Microsoft Outlook or Microsoft Word which I'm
using, where the Tester library would be Microsoft Outlook 11.0 Object Library or Microsoft Office 11.0 Object Library.

To automate multiple versions of Microsoft Office one cannot just import the latest library, ie Microsoft Office 11.0 Object Library
unless you wanted your code to work with Office 2002/2003. However I would like my code to be compatible with Office 2000 as well
which means I cannot import a particular library but would have to rely on the COM-based in process server from the application at
run time. This means I have to write my code using late binding. Late binding means I need to work with objects where Tester would
not be one of them since I won't be importing any Tester libraries, hence my messy code above.


Abubakar said:
objParameters_Late[0] = TesterClosed; /*I get an error here*/

ofcourse its the name of the function, you cant assign the name of the
function to an object. try doing
objParameters_Late[0] = new Tester.CloseEventHandler(TesterClosed);

Ab.
http://joehacker.blogspot.com


Max said:
When you add an event handler to some object you need to specify the
function to call when the event occurs. Suppose there is a
class TesterClosed() which is called whenever the Tester is closed. Code below:

public void TesterClosed(){}

Tester m_tester = new Tester();
m_tester.Close += new Tester.CloseEventHandler(TesterClosed);

Now suppose I want to do this with late binding where I need to work with
objects. The parameter TesterClosed in
Tester.CloseEventHandler(....) is of type "void () type". How do I convert
this into an object that I can pass to the same function
with late binding? Code below which I'm not sure I'm doing right:

//m_tester.Close += new Tester.CloseEventHandler(TesterClosed);

Type objClassType = Type.GetTypeFromProgID("Tester.CloseEventHandler");
object [] objParameters_Late = new object[1];
objParameters_Late[0] = TesterClosed; /*I get an error here*/
object objTesterEvent_Late = Activator.CreateInstance(objClassType, objParameters_Late);

objParameters_Late = new object[1];
objParameters_Late[0] = objTesterEvent_Late;
m_tester_Late.GetType().InvokeMember("Close", BindingFlags.InvokeMethod,
null, m_tester_Late, objParameters_Late);
 
A

Abubakar

objParameters_Late[0] = TesterClosed; /*I get an error here*/

ofcourse its the name of the function, you cant assign the name of the
function to an object. try doing
objParameters_Late[0] = new Tester.CloseEventHandler(TesterClosed);

Ab.
http://joehacker.blogspot.com


Max said:
When you add an event handler to some object you need to specify the
function to call when the event occurs. Suppose there is a
class TesterClosed() which is called whenever the Tester is closed. Code below:

public void TesterClosed(){}

Tester m_tester = new Tester();
m_tester.Close += new Tester.CloseEventHandler(TesterClosed);

Now suppose I want to do this with late binding where I need to work with
objects. The parameter TesterClosed in
Tester.CloseEventHandler(....) is of type "void () type". How do I convert
this into an object that I can pass to the same function
with late binding? Code below which I'm not sure I'm doing right:

//m_tester.Close += new Tester.CloseEventHandler(TesterClosed);

Type objClassType = Type.GetTypeFromProgID("Tester.CloseEventHandler");
object [] objParameters_Late = new object[1];
objParameters_Late[0] = TesterClosed; /*I get an error here*/
object objTesterEvent_Late = Activator.CreateInstance(objClassType, objParameters_Late);

objParameters_Late = new object[1];
objParameters_Late[0] = objTesterEvent_Late;
m_tester_Late.GetType().InvokeMember("Close", BindingFlags.InvokeMethod,
null, m_tester_Late, objParameters_Late);
 

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

Top