泛型类无法接受具有直接行到类型限制的泛型的对象

时间:2012-03-14 16:24:33

标签: c# generics

我有一个c#数据类,定义了一个如下所示的继承链:

public abstract class EntityBaseCore
public abstract class EntityBase : EntityBaseCore
public partial class AdmSite : EntityBase

这样定义的泛型类:

public abstract class ViewModelSecurityBase<T> : Screen, where T : EntityBaseCore
public abstract class EditorViewModelBase<T> : ViewModelSecurityBase<T> where T : EntityBaseCore

我的视图模型类如下所示:

public class SiteViewModel : EditorViewModelBase<AdmSite>

失败
The type 'Yargo.DataModel.AdmSite' cannot be used as type parameter 'T' in the generic type or method 'Yargo.Common.ViewModel.EditorViewModelBase'. There is no implicit reference conversion from 'Yargo.DataModel.AdmSite' to 'Yargo.DataModel.EntityBaseCore'

AdmSite和EntityBaseCode之间的继承链似乎是直接的。我不明白为什么会失败。

1 个答案:

答案 0 :(得分:1)

以下代码似乎对我有用。

public class Screen { }
public abstract class EntityBaseCore
{    }

public abstract class EntityBase : EntityBaseCore
{    }
public partial class AdmSite : EntityBase
{    }

public abstract class ViewModelSecurityBase<T> : Screen where T : EntityBaseCore
{    }
public abstract class EditorViewModelBase<T> : ViewModelSecurityBase<T> where T : EntityBaseCore
{    }

public class SiteViewModel : EditorViewModelBase<AdmSite>
{    }