Wednesday, December 31, 2008

Nontransitive generic interfaces

We're pretty busy racing to a deadline, so I don't have a great deal of time to dig into this. I just stumbled across a C# feature that works differently than I would have guessed. Check out the following code.


public class test
{
   public void doTest()
   {
      Interface1<object> win = new Class1<object>();
      Interface2 fail = new Class1<object>();
   }
}

public interface Interface2 : Interface1<object>
{
}

public interface Interface1<t> where T : class
{
}

public class Class1<t> : Interface1<t>
   where T : class
{

}


All I am really after is for Interface2 to act as an alias for Interface1 and hide the generic declaration bits. Well ... here it is. Maybe I'll come back later if I figure out a solution.

No comments: