所以在前面的一个问题中,我问到一个通用的接口是用一个public class和bingo来实现的.然而,我想要传递的类型之一是内置的可空类型之一,如:int,Guid,String等.
这是我的界面:
- public interface IoUrTemplate<T,U>
- where T : class
- where U : class
- {
- IEnumerable<T> List();
- T Get(U id);
- }
所以当我实现这样的时候:
- public class TestInterface : IoUrTemplate<MyCustomClass,Int32>
- {
- public IEnumerable<MyCustomClass> List()
- {
- throw new NotImplementedException();
- }
- public MyCustomClass Get(Int32 testID)
- {
- throw new NotImplementedException();
- }
- }
我收到错误消息:类型“int”必须是引用类型,以便在通用类型或方法’TestApp.IoUrTemplate’中将其用作参数’U’
我试图推断Int32的类型,但同样的错误.有任何想法吗?
解决方法
我不会这样做,但它可能是让它上班的唯一方法.
- public class MyWrapperClass<T> where T : struct
- {
- public Nullable<T> Item { get; set; }
- }
- public class MyClass<T> where T : class
- {
- }