The type 'T' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method 'System.Data.Linq.Table<TEntity>'

ErrorMessage:The type 'T' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method 'System.Data.Linq.Table<TEntity>' 

Solution1:

错误:private void GetOrDefaultUDC<T>(Table<T> table, string what, int whatID, int mapID, out int id, out Object value)
            where T: IUDC, new()

错误:private void GetOrDefaultUDC<T>(Table<T> table, string what, int whatID, int mapID, out int id, out Object value)
where T: class, IUDC, new()

说明:关键词是reference,见到它就补一个class(尽管从IUDC派生多半已经是class了)。这里出现此约束的原因是在Table的声明中,声明了凡是Table<T>中的T必须是个class,具体声明是:public sealed class Table<TEntity> : IQueryProvider, ITable, IListSource, ITable<TEntity>, IQueryable<TEntity>, IEnumerable<TEntity>, IQueryable, IEnumerable where TEntity : class

 ErrorMessage:c# generics error: The constraints for type parameter '

Solution2:

getting the following error:

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.

class:

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

interface:

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

3 Answers

You are restricting your T generic parameter to class in your implementation. You don't have this constraint on your interface.

You need to remove it from your class or add it to your interface to let the code compile:

Since you are calling the method CreateObjectSet<T>(), which requires the class constraint, you need to add it to your interface.

public interface IMyClass
{
    int GetCount<T>(string filter) where T : class;
}
 
    
hey Dutchie goed man –  user603007 Aug 7 '12 at 12:39
    
Er lopen hier best wat Nederlanders rond inderdaad! :) –  Wouter de Kort Aug 7 '12 at 12:40
    
hier in OZ wat minder :) thanks anyway –  user603007 Aug 7 '12 at 12:59

You either need to apply the constraint to the interface method as well or remove it from the implementation.

You are changing the interface contract by changing the constraint on the implementation - this isn't allowed.

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

You need to constrain your interface, too.

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

出处:http://www.cnblogs.com/dairongle/archive/2011/04/02/2401275.html

参考:http://stackoverflow.com/questions/11845964/c-sharp-generics-error-the-constraints-for-type-parameter-t-of-method

posted @ 2015-01-30 09:53  邹邹  Views(2612)  Comments(0)    收藏  举报