【问题标题】:Convert Class<T> Where T : IMySampleInterface to Class<IMySampleInterface >将 Class<T> Where T : IMySampleInterface 转换为 Class<IMySampleInterface >
【发布时间】:2013-03-08 14:53:51
【问题描述】:

是否可以将Class&lt;T&gt; Where T : IMySampleInterface 转换为Class&lt;IMySampleInterface&gt;

例子:

public abstract class AbstractCommunication: ICommunication
{
    private ICommunicationCallback<ICommunication> callback = null;
    public void registerCommunicationCallback<T>(ICommunicationCallback<T> callback) where T : ICommunication
    {
        this.callback =  (ICommunicationCallback<ICommunication>)callback; //<--DOESNT WORK
    }
}

在我的示例中出现以下异常:System.InvalidCastException

编辑:

public interface ICommunicationCallback<T> where T : ICommunication
{
    void onCommunicationCallback(T sender, String data);
}

我为什么要使用这种方式:如果我有一个基类应该实现例如两个回调,那么我可以简单地使用以下内容:

public class TestClass : ICommunicationCallback<TestClass1>, ICommunicationCallback<TestClass2>

TestClass1:

public class TestClass1: AbstractCommunication
{

}

TestClass2:

public class TestClass2: AbstractCommunication
{

}

编辑: “如果 T 始终是一个 ICommunication,那么为什么要保持其通用性?—— Davin Tryon 20 分钟前”我锁定在这一点上

好的,如果没有泛型,我得到了相同的错误,但不同 System.InvalidCastException: (这就是我一开始使用泛型的原因——我现在记得了)

 public class DocumentTest : ICommunicationCallback<GetDocumentData>
    {
     public void callAsync()
{
     CommunicationFactory comFactory = new CommunicationFactory(communicationServiceClient);
                GetDocumentData getDocumentData = (GetDocumentData)comFactory.createCommunicationObject(CommunicationFactory.ENTITY_TYPE.GET_DOCUMENT_DATA,(ICommunicationCallback<ICommunication> ) this);

}

}

public interface ICommunicationCallback<ICommunication> 
{
    void onCommunicationCallback(ICommunication sender, String data);
}

我认为使用泛型是我的唯一解决方案 - 请参阅: “此功能仅适用于泛型接口和委托。如果您实现变体泛型接口,则实现类仍然是不变的。类和结构不支持 C# 4.0 中的变体。 因此以下内容无法编译: // List 实现协变接口 // IEnumerable。但是类是不变的。 列表列表 = 新列表(); // 这里的编译器错误。” (http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx)

【问题讨论】:

标签: c#


【解决方案1】:
interface ICommunication
{
}

interface ICommunicationCallback<T>
{
}

class AbstractCommunicationCallback<T> : ICommunicationCallback<T>
{

}

abstract  class AbstractCommunication : ICommunication
{
    private ICommunicationCallback<ICommunication> callback = null;
    public void registerCommunicationCallback<T>(ICommunicationCallback<T> callback) where T : ICommunication
    {
        this.callback = (ICommunicationCallback<ICommunication>)callback; //<--DOESNT WORK
    }
}

class Communication : AbstractCommunication { 
}

及测试方法

public void Test()
    {
          AbstractCommunication comm = new Communication();
         AbstractCommunicationCallback<ICommunication> callback = new AbstractCommunicationCallback<ICommunication>();
        comm.registerCommunicationCallback(callback);
    }

在 .net 框架 4 上与 2010 相比没有错误

编辑:一些有趣的信息http://msdn.microsoft.com/en-us/library/ee207183.aspx

【讨论】:

    【解决方案2】:

    我解决了我的问题。但我没有使用泛型 - 我使用 ICommunication 接口。

    public interface ICommunicationCallback
    {
        void onCommunicationCallback(ICommunication sender, CommunicationResultObject result);
    }
    

    在我看来,这个解决方案并不优雅,但它确实有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-03
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      相关资源
      最近更新 更多