Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

c# - How to use same interface two times with diferrent template parameters, in an interface?

I Think it would be more clearer with this example. We Want to see two methods with diferrent parameters in the processor class. "int Process (int value);" "double Process (double value);"

But compiler says for IRoot : 'Generics.IRoot' cannot implement both 'Generics.IProcess' and 'Generics.IProcess' because they may unify for some type parameter substitutions.

public class Processor : IRoot<int, double, int, double>
{
    // Here we want 2 methods
    public int Process(int item) { }
    public double Process(double item) { }
}

public interface IProcess<TResult, TItem>
{
    TResult Process(TItem item);
}

public interface IRoot<TR1, TR2, TItem1, TItem2> :
    IProcess<TR1, TItem1>,
    IProcess<TR2, TItem2>
{

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's my solution. It's based on using differentiation so you can be clear about which interface you want. You have to add an otherwise unused parameter, but that's what tells it which you want.

public interface First { }
public interface Second { }

public class Processor : IRoot<int, double, int, double>
{
    // Here we want 2 methods
    public int Process ( int item ) { System.Console.WriteLine ( "int Process" ); return item + 1; }
    public double Process ( double item ) { System.Console.WriteLine ( "double Process" ); return item + 10.748; }
}

public class TestProcessor : IRoot<int, int, int, int>
{
    int IProcessWithDifferentiator<int, int, First>.Process ( int item )
    {
        System.Console.WriteLine ( "int Process" ); return item + 1;
    }
    int IProcessWithDifferentiator<int, int, Second>.Process ( int item )
    {
        System.Console.WriteLine ( "int Process" ); return item + 100302;
    }
}

public interface IProcessWithDifferentiator<TResult, TItem, TDiff>
{
    TResult Process ( TItem item );
}

public interface IRoot<TR1, TR2, TItem1, TItem2> :
    IProcessWithDifferentiator<TR1, TItem1, First>,
    IProcessWithDifferentiator<TR2, TItem2, Second>
{

}

class Program
{
    static void Main ( string [] args )
    {
        Processor p = new Processor ();
        IProcessWithDifferentiator<int, int, First> one = p;
        System.Console.WriteLine ( "one.Process(4) = " + one.Process ( 4 ) );
        IProcessWithDifferentiator<double, double, Second> two = p;
        System.Console.WriteLine ( "two.Process(5.5) = " + two.Process ( 5.5 ) );

        TestProcessor q = new TestProcessor ();
        IProcessWithDifferentiator<int, int, First> q1 = q;
        System.Console.WriteLine ( "q1.Process(4) = " + q1.Process ( 4 ) );
        IProcessWithDifferentiator<int, int, Second> q2 = q;
        System.Console.WriteLine ( "q2.Process(5) = " + q2.Process ( 5 ) );

        System.Console.ReadLine ();
    }
}

Here's the output.

int Process
one.Process(4) = 5
double Process
two.Process(5.5) = 16.248
int Process
q1.Process(4) = 5
int Process
q2.Process(5) = 100307

This will work even if you use IRoot<int,int,int,int> as you can see above; it knows (because we tell it) which IDifferentiatedProcess to use.

(In case it matters, I'm on Visual Studio 2012.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...