0

Im trying to implement CRTP interface to my code, but the constraint make me stuck. how to implement the constraints if i have code structure look like this? Is this legal? Thank you.

interface IInterface<T>
    where T: IInterface<T>
{
    //bla bla bla
    T Member { get; set; }
}
interface ITest1<iTest2, iTest1> : IInterface<iTest2>
{
    //bla bla bla
}
interface ITest2<iTest1, iTest3> : IInterface<iTest1>
{
    iTest3 RefMember { get; set; }
    //bla bla bla
}
interface ITest3<iTest2>
{
    List<iTest2> manyTest { get; set; }
    //bla bla bla
}
class Test1 : ITest1<Test2, Test1>
{
    //bla bla bla
}
class Test2 : ITest2<Test1, Test3>
{
    //bla bla bla
}
class Test3 : ITest3<Test2>
{
    //bla bla bla    
}
user2277061
  • 7
  • 1
  • 4
  • Have you tried to compile your code? If it doesn't compile, what error are you getting? – svick Apr 20 '13 at 23:09
  • that code is all wrong :) even if compiler allows it (`interface IInterface where T : IInterface` et al). I think you best explain what you want, structure you have. – NSGaga-mostly-inactive Apr 21 '13 at 01:05
  • @svick if the code written like that, i have no any error. But when i trying to adding the constraints, will make me headache .`ITest1 : Entity where iTest2 : ITest2,ITest3,..... (?????)` – user2277061 Apr 21 '13 at 09:46
  • @NSGaga i has reading Eric Lippert blog, he said : > `class Blah where T : Blah` > That appears to be circular in (at least) two ways. Is this really legal? > Yes it is legal, and it does have some legitimate uses... In my case, i has 2 player, each player has 10 pawns. And each pawn will be placed on a board size of 5 x 5. Every Point of my board has different move references. the mapping like this. Every pawn will copying move references from their point. http://thumbnails101.imagebam.com/25012/afd0fd250112805.jpg?nc – user2277061 Apr 21 '13 at 09:48
  • 1
    @user2277061 That doesn't sound like something you should model using the type system, I think. – svick Apr 21 '13 at 10:30
  • I agree - it may be 'legit' in extreme cases (and compiles) - but you seem to have a tree like structure - and you may need a specific structure. But I don't see many `types` there, except for Pawn, Point, and not even what should be generic. – NSGaga-mostly-inactive Apr 21 '13 at 10:49
  • @svick Ya, thats all my wrong, i think i can connecting some object with same interface using that way. But i got many error. I think that i can create an interface, where they will have member as the same interface. – user2277061 Apr 22 '13 at 03:55
  • @NSGaga i must redesign my structure, i think. – user2277061 Apr 22 '13 at 03:57

1 Answers1

2
 public abstract class MyBase
{
    /// <summary>
    /// The my test method. divyang
    /// </summary>
    public virtual void MyVirtualMethodWhichIsOverridedInChild()
    {
        Console.Write("Method1 Call from MyBase");
    }

    /// <summary>
    /// The my another test method.
    /// </summary>
    public abstract void MyAnotherTestMethod();

    /// <summary>
    /// The my best method.
    /// </summary>
    public virtual void MyVirualMethodButNotOverridedInChild()
    {
        Console.Write("Method2 Call from MyBase");
    }
}

now make base class

public abstract class CrtpBaseWrapper<T> : MyBase
    where T : CrtpBaseWrapper<T>
{
}

then you can make your subclass

public class CrtpChild : CrtpBaseWrapper<CrtpChild>
{
    /// <summary>
    /// The my test method. divyang
    /// </summary>
    public override void MyVirtualMethodWhichIsOverridedInChild()
    {
        Console.Write("Method1 Call from CrtpChild");
    }

    /// <summary>
    /// The my another test method.
    /// </summary>
    public override void MyAnotherTestMethod()
    {
        Console.Write("MyAnotherTestMethod Call from CrtpChild");
    }
}
divyang4481
  • 1,584
  • 16
  • 32