General methods with restrictions that are common

What I want to do is a method that uses a generic type as a constrained parameter. However, the constraint type also has a second generic type, but I want this method to work no matter what the second imprint is:

public class IEvent<T> where T : EventArgs { }
public class EventManager
{
    public void DoMethod<T>() where T: IEvent<???>
    {
    }
}

In particular, I try to get my class to EventManagerreceive some kind of event, and then do something with it. Am I embarrassing something, or is it doable?

+5
source share
2 answers

You must use the second restriction:

void DoMethod<TEvent, TArgs>() where TEvent : IEvent<TArgs> where TArgs : EventArgs {}
+11
source

Try it.

public class IEvent<T> where T : EventArgs { }
public class EventManager
{
    public void DoMethod<T, U>() where T : IEvent<U> where U : EventArgs
    {
    }
}
+1
source

All Articles