微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 在通用接口中实现Nullable类型

所以在前面的一个问题中,我问到一个通用的接口是用一个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 
{

}

原文地址:https://www.jb51.cc/csharp/92656.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐