C
COLIN JACK
Hi All,
I've got a situation where I'm implementing an interface (BaseInterface in
example below) and I want to use explicity interface implementation of an
event so that I can add type safety. To see what I mean look at the example
below where the class implementing the interface actually wants the event to
be for a more specific delegate.
Now this seems to work but the code, to me is unnecessarily ugly. This
leaves me wondering if this is something other people do and if so is there
a better way of doing it?
I should also say the reason I'm doing it is in a real situation I want to
have a class implementing an interface raising the event with EventArguments
that are derived from the ones that the delegate in the interface uses,
doing so stops me having to cast the event arguments in the event handler.
Thanks in advance,
Colin Jack
public delegate void GeneralEventHandler(object sender, GeneralEventArgs
ex);
public delegate void SpecificEventHandler(object sender, SpecificEventArgs
ex);
public class GeneralEventArgs : EventArgs
{
public string general = "General";
}
public class SpecificEventArgs : GeneralEventArgs
{
public string specific = "Specific";
}
public interface BaseInterface
{
event GeneralEventHandler GeneralEvent;
}
public class ConcreteClassSpecific: BaseInterface
{
// explicit interface implementation, I need to actually specify
// the add/remove accessors
GeneralEventHandler specificEvent;
event InheritanceTest.GeneralEventHandler BaseInterface.GeneralEvent
{
add
{
specificEvent += value;
}
remove
{
specificEvent -= value;
}
}
// This means that subscribers have to use SpecificEventHandler which
// allows them to access its specific string field
public event SpecificEventHandler GeneralEvent;
protected void OnEvent()
{
if (specificEvent != null)
{
specificEvent(null, new GeneralEventArgs());
}
if (GeneralEvent != null)
{
GeneralEvent(this, new SpecificEventArgs());
}
}
public void RaiseEvent()
{
OnEvent();
}
}
I've got a situation where I'm implementing an interface (BaseInterface in
example below) and I want to use explicity interface implementation of an
event so that I can add type safety. To see what I mean look at the example
below where the class implementing the interface actually wants the event to
be for a more specific delegate.
Now this seems to work but the code, to me is unnecessarily ugly. This
leaves me wondering if this is something other people do and if so is there
a better way of doing it?
I should also say the reason I'm doing it is in a real situation I want to
have a class implementing an interface raising the event with EventArguments
that are derived from the ones that the delegate in the interface uses,
doing so stops me having to cast the event arguments in the event handler.
Thanks in advance,
Colin Jack
public delegate void GeneralEventHandler(object sender, GeneralEventArgs
ex);
public delegate void SpecificEventHandler(object sender, SpecificEventArgs
ex);
public class GeneralEventArgs : EventArgs
{
public string general = "General";
}
public class SpecificEventArgs : GeneralEventArgs
{
public string specific = "Specific";
}
public interface BaseInterface
{
event GeneralEventHandler GeneralEvent;
}
public class ConcreteClassSpecific: BaseInterface
{
// explicit interface implementation, I need to actually specify
// the add/remove accessors
GeneralEventHandler specificEvent;
event InheritanceTest.GeneralEventHandler BaseInterface.GeneralEvent
{
add
{
specificEvent += value;
}
remove
{
specificEvent -= value;
}
}
// This means that subscribers have to use SpecificEventHandler which
// allows them to access its specific string field
public event SpecificEventHandler GeneralEvent;
protected void OnEvent()
{
if (specificEvent != null)
{
specificEvent(null, new GeneralEventArgs());
}
if (GeneralEvent != null)
{
GeneralEvent(this, new SpecificEventArgs());
}
}
public void RaiseEvent()
{
OnEvent();
}
}