简体   繁体   English

在通用接口中实现可空类型

[英]Implementing Nullable Types in Generic Interface

So in a previous question I asked about implementing a generic interface with a public class and bingo, it works. 因此,在上一个问题中,我询问了如何使用公共类和宾果实现通用接口,它可以工作。 However, one of the types I'm looking to pass in is one of the built in nullable types such as: int, Guid, String, etc. 但是,我想要传入的其中一种类型是内置的可空类型之一,例如:int,Guid,String等。

Here's my Interface: 这是我的界面:

public interface IOurTemplate<T, U>
    where T : class
    where U : class
{
    IEnumerable<T> List();
    T Get(U id);
}

So when I implement this like so: 所以,当我这样实现时:

public class TestInterface : IOurTemplate<MyCustomClass, Int32>
{
    public IEnumerable<MyCustomClass> List()
    {
        throw new NotImplementedException();
    }

    public MyCustomClass Get(Int32 testID)
    {
        throw new NotImplementedException();
    }
}

I receive the error message: The type 'int' must be a reference type in order to use it as parameter 'U' in the generic type or method 'TestApp.IOurTemplate' 我收到错误消息: 类型'int'必须是引用类型才能在泛型类型或方法'TestApp.IOurTemplate'中将其用作参数'U'

I've tried to infer the type Int32?, but same error. 我试图推断类型Int32?,但同样的错误。 Any ideas? 有任何想法吗?

I wouldn't really do this but it's probably the only way to get it to work. 我不会真的这样做,但它可能是让它发挥作用的唯一方法。

public class MyWrapperClass<T> where T : struct 
{
    public Nullable<T> Item { get; set; }   
}

public class MyClass<T> where T : class 
{

}

Nullable types don't satisfy class or struct constraints: 可空类型不满足classstruct约束:

C# Language Specification v3.0 (Section §10.1.5: Type parameter constraints): C#语言规范v3.0(第10.1.5节:类型参数约束):

The reference type constraint specifies that a type argument used for the type parameter must be a reference type. 引用类型约束指定用于type参数的类型参数必须是引用类型。 All class types, interface types, delegate types, array types, and type parameters known to be a reference type (as defined below) satisfy this constraint. 已知为引用类型(如下定义)的所有类类型,接口类型,委托类型,数组类型和类型参数都满足此约束。 The value type constraint specifies that a type argument used for the type parameter must be a non-nullable value type. 值类型约束指定用于type参数的类型参数必须是非可空值类型。

All non-nullable struct types, enum types, and type parameters having the value type constraint satisfy this constraint. 具有值类型约束的所有非可空结构类型,枚举类型和类型参数都满足此约束。 Note that although classified as a value type, a nullable type (§4.1.10) does not satisfy the value type constraint. 请注意,虽然归类为值类型,但可空类型(第4.1.10节)不满足值类型约束。 A type parameter having the value type constraint cannot also have the constructor-constraint. 具有值类型约束的类型参数也不能具有构造函数约束。

Any reason why you need to restrict type U to class? 您需要将类型U限制为类的任何原因?

public interface IOurTemplate<T, U>
    where T : 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();
    }
}

FYI: int? 仅供参考: int? is the C# shorthand for Nullable<int> . Nullable<int>的C#简写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM