2012-03-14 44 views
3

我有一個看起來像這樣的繼承鏈定義的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

您是否在不同的命名空間中有兩個名爲AdmSite的類? – 2012-03-14 16:35:25

+0

我在對象瀏覽器中搜索了名稱,它只找到一個類(交叉編譯到桌面和Silverlight程序集中)。有問題的代碼在Silverlight的結尾。 – Elton 2012-03-14 16:39:02

+0

如果您不繼承'ViewModelSecurityBase ',該怎麼辦?它工作嗎? – Seb 2012-03-14 17:22:35

回答

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> 
{ } 
+0

那麼,EntityBaseCore,EntityBase和SiteViewModel都在不同的程序集中,但引用似乎對我來說是正確的。 Intellisense似乎正確地選擇了它們,但編譯器似乎正在迷失。 – Elton 2012-03-14 16:44:20

+0

我將EntityBaseCore分解爲一個接口,並將泛型限制在該接口中。所有的作品現在似乎都在一起。謝謝你的幫助。我愚蠢的是沒有嘗試過你所做的。 – Elton 2012-03-15 18:06:02