c# – 在通用接口中实现Nullable类型
发布时间:2020-12-15 04:08:24 所属栏目:百科 来源:网络整理
导读:所以在前面的一个问题中,我问到一个通用的接口是用一个public class和bingo来实现的.然而,我想要传递的类型之一是内置的可空类型之一,如:int,Guid,String等. 这是我的界面: public interface IOurTemplateT,U where T : class where U : class{ IEnumerabl
所以在前面的一个问题中,我问到一个通用的接口是用一个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 { } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |