2012-08-07 15 views
5

次のエラーを取得:ジェネリックスエラー:メソッドの型パラメータ 'T'の制約...?

Error 1 The constraints for type parameter ' T ' of method
' genericstuff.Models.MyClass.GetCount<T>(string) ' must match the constraints for type
parameter ' T ' of interface method ' genericstuff.IMyClass.GetCount<T>(string) '. Consider
using an explicit interface implementation instead.

クラス:

public class MyClass : IMyClass 
{ 
    public int GetCount<T>(string filter) 
    where T : class 
     { 
     NorthwindEntities db = new NorthwindEntities(); 
     return db.CreateObjectSet<T>().Where(filter).Count(); 
     } 
} 

インターフェース:あなたの実装では、クラスにあなたのTジェネリックパラメータを制限している

public interface IMyClass 
{ 
    int GetCount<T>(string filter); 
} 

答えて

16

。インタフェースにこの制約はありません。

は、あなたのクラスからそれを削除するか、コードのコンパイルをできるようにあなたのインターフェースに追加する必要があります。

あなたは、あなたがあなたのインターフェイスに追加する必要がありrequires the class constraint方法CreateObjectSet<T>()を呼んでいるので。

public interface IMyClass 
{ 
    int GetCount<T>(string filter) where T : class; 
} 
+0

hey Dutchie goed man – user603007

+0

あなたは最高のワットNederlanders rond inderdaad! :) –

+0

オハイオ州ワット・ミンダーの高校:ありがとうとにかく – user603007

3

インターフェイスメソッドにも制約を適用するか、実装から削除する必要があります。

実装の制約を変更することでインタフェース契約を変更していますが、これは許可されていません。

public interface IMyClass 
{ 
    int GetCount<T>(string filter) where T : class; 
} 
1

また、インターフェイスも制約する必要があります。

public interface IMyClass 
{ 
    int GetCount<T>(string filter) where T : class; 
} 
関連する問題