I've been trying to create a generic event. Basically it should look like this:
namespace DelegateTest
{
class Program
{
static void Main(string[] args)
{
var lol = new SomeClass();
lol.SomeEvent += handler;
}
static void handler(object sender, SomeDerivedClass e)
{
}
}
class SomeClass
{
public delegate void SomeEventDelegate<in T>(object sender, T data);
public event SomeEventDelegate<ISomeInterface> SomeEvent;
}
interface ISomeInterface
{
}
class SomeDerivedClass : ISomeInterface
{
}
}
I want to allow the user to pass any delegate which's second parameter is derived from "ISomeInterface."
"in" specifies contra-variance, right? That means if the API is expecting something more general, you can pass it something more specific (in my base "ISomeInterface" would be general, and my "SomeDerivedClass" would be specific.) I am, however, being told my the compiler that "no overload for method handler matches DelegateTest.SomeClass.SomeEventDelegate."
I am wondering why this isn't working. What are the problems that would be caused if it was? Or am I missing something for it to work?
Thanks in advance!