473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Getting a pointer to a method (as IntPtr)

How does one go about getting a pointer to method as an IntPtr?

Example:

public class A
{
public void X()
{
Y(Z);
}

public void Y(IntPtr z)
{
}

public void Z()
{
}
}

This won't compile, because the compiler thinks I meant Y(Z()).

I'm guessing this must be possible, because classes inheriting from
System.EventHandler have ctors which takes a parameter called 'method',
whose type is IntPtr.

Cheers,
Rik
Nov 16 '05 #1
5 13411
Hi....

You would use a delegate.

John

"Rik Hemsley" <ri*@rikkus.info> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
How does one go about getting a pointer to method as an IntPtr?

Example:

public class A
{
public void X()
{
Y(Z);
}

public void Y(IntPtr z)
{
}

public void Z()
{
}
}

This won't compile, because the compiler thinks I meant Y(Z()).

I'm guessing this must be possible, because classes inheriting from
System.EventHandler have ctors which takes a parameter called 'method',
whose type is IntPtr.

Cheers,
Rik

Nov 16 '05 #2
What you want is a delegate. There are no pointers, per se. Nothing you
could use from unmanaged code.

Here's a description with samples:

http://www.akadia.com/services/dotne...nd_events.html

Pete

"Rik Hemsley" <ri*@rikkus.info> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
How does one go about getting a pointer to method as an IntPtr?

Example:

public class A
{
public void X()
{
Y(Z);
}

public void Y(IntPtr z)
{
}

public void Z()
{
}
}

This won't compile, because the compiler thinks I meant Y(Z()).

I'm guessing this must be possible, because classes inheriting from
System.EventHandler have ctors which takes a parameter called 'method',
whose type is IntPtr.

Cheers,
Rik

Nov 16 '05 #3
Pete Davis wrote:
What you want is a delegate.
See the ctor for System.Timers.ElapsedEventHandler.

I'm trying to instantiate that class (and any other EventHandler-derived
class) using ConstructorInfo.Invoke.

I am writing a unit test framework which allows testing of async code,
so tricky things like this are necessary.
There are no pointers, per se. Nothing you could use from
unmanaged code.


I'm using managed code only.

Quoting myself:

I'm guessing this must be possible, because classes inheriting from
System.EventHandler have ctors which takes a parameter called 'method',
whose type is IntPtr.

EventHandler-derived classes must be instantiated somehow. I just need
to know how!

Cheers,
Rik
Nov 16 '05 #4
Rik,

When creating delegates dynamically, you don't call the Invoke method on
the ConstructorInfo instance. Rather, you call the appropriate static
CreateDelegate method overload, which will take the Type information for the
delegate, the method info which you want to attach to, the instance, etc,
etc.

Once you have that, you can dynamically invoke your delegate calling the
DynamicInvoke method on the instance of the delegate.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Rik Hemsley" <ri*@rikkus.info> wrote in message
news:Ou**************@TK2MSFTNGP09.phx.gbl...
Pete Davis wrote:
What you want is a delegate.


See the ctor for System.Timers.ElapsedEventHandler.

I'm trying to instantiate that class (and any other EventHandler-derived
class) using ConstructorInfo.Invoke.

I am writing a unit test framework which allows testing of async code, so
tricky things like this are necessary.
There are no pointers, per se. Nothing you could use from
unmanaged code.


I'm using managed code only.

Quoting myself:

I'm guessing this must be possible, because classes inheriting from
System.EventHandler have ctors which takes a parameter called 'method',
whose type is IntPtr.

EventHandler-derived classes must be instantiated somehow. I just need to
know how!

Cheers,
Rik

Nov 16 '05 #5
Nicholas Paldino [.NET/C# MVP] wrote:
Rik,

When creating delegates dynamically, you don't call the Invoke method on
the ConstructorInfo instance. Rather, you call the appropriate static
CreateDelegate method overload, which will take the Type information for the
delegate, the method info which you want to attach to, the instance, etc,
etc.

Once you have that, you can dynamically invoke your delegate calling the
DynamicInvoke method on the instance of the delegate.

Hope this helps.


That's exactly what I was looking for, thanks very much.

Rik
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ken Kast | last post by:
Here's my situation. I have an object function Obj () { this.foo = null; } a function function bar() {...} another function
2
by: Dirk | last post by:
Hello I override the WndProc method of a control-derived class and want to handle the WM_NOTIFY message. The following code leads to a NMHDR pointer that points to something where all members...
4
by: astro | last post by:
I would like to build some generic code that is able to figure out the correct dataAdapter to apply changes to given a form with several dataAdapters. Any suggestions on the following? Thank...
4
by: Abra | last post by:
I have an application where I need to send a inter-process message (a data stream) that contains among other the address of a function (member of a class). For that, I need to serialize it, so I...
2
by: ebiweb | last post by:
hi all, I need to send the address of a method to .dll function. but i don't know how can i get the address of a method ?! :( please help me. thnx a lot.
1
by: =?Utf-8?B?VmlrcmFt?= | last post by:
is there any object available in c#/asp.net which can give me event name in that eevnt only. Like if I am in Page_load event, i should get Page_Load or if I am in Button_click event I should get...
7
by: Maciej Oszutowski | last post by:
Hi, I'm going to port my PE manipulation library (written in C) to managed c++ class library. I would like to have opportunity to read content not only from files, but also from memory, for...
7
by: Andrus | last post by:
How to get syntactically correct signature which compiles for code template grneration ? I tried code below but it creates syntactically incorrect signature. Andrus. using System; using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.