2012-08-07 17 views
5

consiguiendo el error siguiente:C# genéricos error: Las restricciones para el tipo de parámetro 'T' del método ...?

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.

Clase:

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

Interfaz:

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

Respuesta

16

Usted está restringiendo el parámetro genérico T a clase en su aplicación. Usted no tiene esta restricción en su interfaz.

Necesitas sacarlo de su clase o añadirlo a su interfaz para que el código de compilación:

Desde que está llamando el método CreateObjectSet<T>(), que requires the class constraint, es necesario añadirlo a su interfaz.

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

hey Dutchie goed man – user603007

+0

Er lopen hier best wat Nederlanders rond inderdaad! :) –

+0

hier en OZ wat minder :) gracias de todos modos – user603007

3

También debe aplicar la restricción al método de interfaz o eliminarlo de la implementación.

Está cambiando el contrato de la interfaz cambiando la restricción en la implementación; esto no está permitido.

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

También debe restringir la interfaz.

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