开发者

How to control implementation of [dependent generic interfaces]

开发者 https://www.devze.com 2023-04-03 20:54 出处:网络
Please read 2nd Edition first. Let\'s suppose that we have two dependent generic interfaces: interface ITemplate1<T1, T2>

Please read 2nd Edition first.


Let's suppose that we have two dependent generic interfaces:

interface ITemplate1<T1, T2>
    where T1 : ITemplate1<T1, T2>
    where T2 : ITemplate2<T1, T2>
{
    T2 t2 { get; set; }
}

interface ITemplate2<T1, T2>
    where T1 : ITemplate1<T1, T2>
    where T2 : ITemplate2<T1, T2>
{
    T1 t1 { get; set; }
}

And from both of them two different classes have been implemented:

class Class1_1 : ITemplate1<Class1_1, Class2_1>
{
    public Class2_1 t2
    {
        get;
        set;
    }
}

class Class1_2 : ITemplate1<Class1_2, Class2_2>
{
    public Class2_2 t2
    {
        get;
        set;
    }
}

class Class2_1 : ITemplate2<Class1_1, Class2_1>
{
    public Class1_1 t1
    {
        get;
        set;
    }
}
开发者_JAVA百科
class Class2_2 : ITemplate2<Class1_2, Class2_2>
{
    public Class1_2 t1
    {
        get;
        set;
    }
}

Now, I want to define a class which implement ITemplate1 regardless of its required types..

class MyClass<T> where T : ITemplate1< ? , ? >
{
    ...
}

..to prevent from declaring more than one class as:

class MyClass1<T> where T : ITemplate1<Class1_1, Class2_1>
{
    ...
}


class MyClass2<T> where T : ITemplate1<Class1_2, Class2_2>
{
    ...
}

How can I achieve that? (i.e. Real problem may involved in more than two classes!) Thanks in advance.


1st Edition: Class1_1 & Class2_1 as well as Class1_2 & Class2_2 must be used together. In other words, I'm looking for a way to have:

class MyClass<T>
    where T : ITemplate1<Class1_1, Class2_1>
    or
    where T : ITemplate1<Class1_2, Class2_2>
{
    ...
}

2nd Edition: I think I could ask this question simpler to avoid to put dear readers into inconvenience. So I do apologize if I mixed you up. The reason for asking such a confusing question is that the real problem is completely complicated. Any way, here is the simple form:

Title: How to control implementation of interfaces?

Descriptions: I have a class which implements two dependent interfaces. I need to control given types, because only some pair of types are consistent with each other. --> 1st Edition


I don't know excactly if you are searching for exactly this, but you can make your ClassX_y more generic for example like:

    class Class1_x<T2> : ITemplate1<Class1_x<T2>, T2>
        where T2 : ITemplate2<Class1_x<T2>, T2>
    {
        public T2 t2
        {
            get; set;
        }
    }

    class Class2_x<T1> : ITemplate2<T1, Class2_x<T1>>
        where T1 : ITemplate1<T1, Class2_x<T1>>
    {
        public T1 t1
        {
            get;
            set;
        }
    }

Or something like this:

class Class1_xy<T1,T2> : ITemplate1<T1,T2>
    where T2 : ITemplate2<T1,T2>
    where T1 : ITemplate1<T1,T2>
{
    public T2 t2
    {
        get; set; }
}

class Class2_xy<T1, T2> : ITemplate2<T1, T2>
    where T2 : ITemplate2<T1, T2>
    where T1 : ITemplate1<T1, T2>
{
    public T1 t1
    { get; set; }
}

class ClassBoth_xy<T1, T2> : ITemplate1<T1,T2>, ITemplate2<T1, T2>
    where T2 : ITemplate2<T1, T2>
    where T1 : ITemplate1<T1, T2>
{
    public T1 t1
    { get; set; }

    public T2 t2
    { get; set; }
}

But I don't see exactly the point in this - maybe you can enlighten us?


Using ClassBoth_xy<T1, T2> is a really good idea but can not restrict MyClass from being implemented by incorrect pairs of <T1, T2>. I solved this issue as:

class MyClass<T1, T2>
    where T1 : ITemplate1<T1, T2>, new()
    where T2 : ITemplate2<T1, T2>, new()
{
    public MyClass()
    {
        T1 _t1 = new T1();
        T2 _t2 = new T2();

        bool isValid = (_t1 is Class1_1 && _t2 is Class2_1) ||
                       (_t1 is Class1_2 && _t2 is Class2_2);

        if( !isValid )
            throw new Exception("Inconsistent types have been used to instantiate MyClass.");
    }

    ...
}
0

精彩评论

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

关注公众号