通用类型头痛:当通用类型从多个接口继承时,传入类型时出现问题

时间:2015-02-25 00:17:18

标签: c# generics

我收到了错误

The type GenericObject<ISomeInterface> must be convertible to <GenericObject<GenericObject<ISomeInterface>> 
in order to use it as parameter <T> in the generic class Wrapper<T>

有问题的课程

public abstract class GenericObject<T> : ReactiveObject where T: class, ISomeInterface

public class SpecificSubObject: GenericObject<SpecificSubObject>, ISomeInterface {}

//T = any potential SpecificSubObject
public class Wrapper<T> : ReactiveObject, ISomeInterface where T : GenericObject<T>, ISomeInterface {}

//View.xaml.cs
public partial class View
{
  public Wrapper<GenericObject<ISomeInterface>> SomeWrapper {get;set;}
}

是否有某种方式(不使我的View类具有通用性(因为部分类不能通用))以使其工作?

1 个答案:

答案 0 :(得分:0)

您可以尝试为Wrapper添加第二个参数:

public class Wrapper<T,P> : ReactiveObject, ISomeInterface 
      where T : GenericObject<P> 
      where P : class, ISomeInterface
{...}

它应该由编译器推断,因此View将保持不变。