开发者

Generics with Event automatically generated

I am trying to create a generic class. The class is going to have some generic events. Is it possible to create generic event handlers something like

class MyClass<T> 
{
    public event EventArgs SomeEventClass();
}
开发者_高级运维

Where the class name for the EventClass is created by generics at the same time using a base class.

I am effectively trying to pass thing back in an event handler and would like them to be type safe to the caller.


Are you wanting something like this....

class MyClass<T> 
{
   public event EventHandler<EventArgs<T>> SomeEvent;

   protected void RaiseEvent(T data)
   {
      var tmp = SomeEvent;
      if(tmp != null)
         tmp(this, new EventArgs<T>(data));
    }
}

You will need to create the generic EventArgs but that should be easy. You can derive the base class and call the RaiseEvent method using the specific generic argument


Names can't be specified with generics in any context. They are use to specify types.

Code generation might be what you are looking for.


Does this do what you want?

class MyClass<T> 
{
    public event Func<T> GenericEvent;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜