形成此通用内部非通用接口调用

时间:2016-12-11 20:34:47

标签: c# generics

public interface IMyClassClient
{
    MyClass<T> Default();
    //T cannot be found, error
}

public class MyClass<T>
{
    private IMyClassClient owner;

    private MyClass<T> test;
    public MyClass<T> Test
    {
        get { return _test;}
        set
        {
           if(value ==owner.Default())
           {
              //how to form this call?^^^^^
              MessageBox.Show("Well that's true");
           }
        }
    }

}

目标是实施IMyClassClient的班级将处理IMyClassClient.Default()

的返回值

我在这里缺少什么?

编辑:

经过一番思考后,以下是我需要完成的更好的措辞:

public interface IMyInterface
{
    //see Instance member in SomeClass, down below? How to declare it here?
}

public class SomeClass:IMyInterface
{
    public MyClass<int> Instance()
    {
         return new myClass<int>(); //for brevity
    }

}

1 个答案:

答案 0 :(得分:1)

你也应该在界面中使用泛型,如果你不这样做,你就不能写MyClass<T> Default(),那里你没有T

public interface IMyClassClient<T>
{
    MyClass<T> Default();
}

public class MyClass<T>
{
    private IMyClassClient<T> owner;

    private MyClass<T> test;
    public MyClass<T> Test
    {
        get { return test; }
        set
        {
            if (value == owner.Default())
            {
                MessageBox.Show("Well that's true");
            }
        }
    }
}

编辑:当然,你可以在非通用接口中声明geeric函数

public interface IMyInterface
{
    MyClass<T> Instance<T>();
}



public class SomeClass : IMyInterface
{
    public MyClass<int> Instance()
    {
        return new myClass<int>(); 
    }
}