Event in interface

G

Guest

Anyone have an example of defining a custom event (i.e custom delegate) in an
interface?
 
N

Nicholas Paldino [.NET/C# MVP]

Anthony,

You mean like this:

public interface IMyInterface
{
public event EventHandler MyEvent;
}

That should give you what you want.

Hope this helps.
 
G

Guest

Yes,

I didn't mention it in the post but where you have EventHandler in your
post, I need to use my own custom delegate. I was having a hard to discerning
where to define the delegate. I finally defined within the namespace and not
at class level at it worked.

Another quick question though. I have a .NET Assembly that will publish an
event and I want to subscribe to it from an MFC C++ application. Do you have
any idea if that is possible or not?

--
Anthony Yott


Nicholas Paldino said:
Anthony,

You mean like this:

public interface IMyInterface
{
public event EventHandler MyEvent;
}

That should give you what you want.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Anthony Yott said:
Anyone have an example of defining a custom event (i.e custom delegate) in
an
interface?
 
G

Guest

Nicholas,

I think the big question (for me) is how to define the delegte type for the
event in the interface so that it is availble to COM clients; otherwise I'm
not sure how the COM client can subscribe to the event if it doesn't know the
event target type.
--
Anthony Yott


Nicholas Paldino said:
Anthony,

You mean like this:

public interface IMyInterface
{
public event EventHandler MyEvent;
}

That should give you what you want.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Anthony Yott said:
Anyone have an example of defining a custom event (i.e custom delegate) in
an
interface?
 
N

Nicholas Paldino [.NET/C# MVP]

Anthony,

You can't (and in most cases, shouldn't) define the delegate type for
the event ^IN^ the interface. Rather, just declare it in the same assembly,
and COM interop should pick it up just fine when creating the interop
library.

As for exporting your event for COM interop, check out the section of
MSDN titled "Exported Member Conversion", located at (watch for line wrap):

http://msdn2.microsoft.com/en-us/library/28w1w83f.aspx#cpcontlbexpmemberconversionanchor3

Basically, you have to define your event interface to implement in COM
to expose through connection points. However, you can't apply the
ComSourceInterfaces attribute to the interface, you have to apply it to the
class that implements the interface exposing the events.

You don't have to use COM interop in order to use the .NET component in
MFC. You could compile with /CLR, but it might give you overhead you don't
want. You could even create a shim in another MFC dll project which would
interact with .NET, and then reference that.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Anthony Yott said:
Nicholas,

I think the big question (for me) is how to define the delegte type for
the
event in the interface so that it is availble to COM clients; otherwise
I'm
not sure how the COM client can subscribe to the event if it doesn't know
the
event target type.
--
Anthony Yott


Nicholas Paldino said:
Anthony,

You mean like this:

public interface IMyInterface
{
public event EventHandler MyEvent;
}

That should give you what you want.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Anthony Yott said:
Anyone have an example of defining a custom event (i.e custom delegate)
in
an
interface?
 

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