简体   繁体   English

该类型不能在泛型类型或方法中用作类型“T”。 没有隐式的引用转换

[英]The type cannot be used as type 'T' in the generic type or method. There is no implicit reference conversion from to

I'm trying to write a generic method to check if List is null or empty and throw an exception. 我正在尝试编写一个泛型方法来检查List是null还是空并抛出异常。

This is what I have: 这就是我所拥有的:

public static void IsNullOrEmpty<T>(T argument) where T : List<T>
{
   if (argument == null || argument.Count == 0)
   {
        throw new ArgumentException("Argument " + nameof(argument) + " can't be null or empty list.");
   }
}

The problem is with calling this method; 问题在于调用此方法; I trying to call this method like this: 我试着像这样调用这个方法:

ThrowIf.Argument.IsNullOrEmpty<List<ParameterServiceReference.Parameter>>(options.Parameters);

Where options.Parameters is type of: List<ParameterServiceReference.Parameter>. 其中options.Parameters的类型为: List<ParameterServiceReference.Parameter>.

I'm getting this error: 我收到这个错误:

There is no implicit reference conversion from 'System.Collections.Generic.List<ParameterServiceReference.Parameter>' to 'System.Collections.Generic.List<System.Collections.Generic.List<ParameterServiceReference.Parameter>>

If I call method like this: 如果我这样调用方法:

ThrowIf.Argument.IsNullOrEmpty<ParameterServiceReference.Parameter>(options.Parameters);

I get this error: 我收到此错误:

cannot convert from 'System.Collections.Generic.List<Project.Layer.ParameterServiceReference.Parameter>' to 'Project.Layer.ParameterServiceReference.Parameter'

Is this kind of generic method possible with generic collection or not, in the solution I will have many generic lists of different classes and I think it's not smart to write so many overloaded methods. 这种泛型方法是否可以使用泛型集合,在解决方案中我将有许多不同类的通用列表,我认为编写这么多重载方法并不聪明。

You generic constraint specifically says that the parameter needs to be some type that is a List where the generic type of the list is itself . 您的泛型约束特别指出参数需要是某种类型,它是列表的泛型类型本身的List。 When you say where T : List<T> you're saying that the generic argument, T , needs to be a List<T> where T is the type you're adding the constraint for. 当你说where T : List<T>你说通用参数T需要是List<T> ,其中T是你为其添加约束的类型。

What you should really do here is just have the method accept a List<T> , not a T where T is a List : 你应该在这里真正做的是让方法接受List<T> ,而不是T ,其中TList

public static void IsNullOrEmpty<T>(List<T> argument) { //...

You can't have a constraint that's T : List<T> , just like you can't have a type MyList that inherits from MyList (or type T that inherits from List<T> ). 你不能有一个约束的T : List<T>就像你不能有一个类型MyList从继承MyList (或类型T ,从继承List<T>

Just use public static void IsNullOrEmpty<T>(List<T> argument) 只需使用public static void IsNullOrEmpty<T>(List<T> argument)

You can have something like this: 你可以这样:

public static void IsNullOrEmpty<S,T>(T argument) where T : List<S>
{
    if (argument == null || argument.Count == 0)
    {
        throw new ArgumentException("Argument " + nameof(argument) + " can't be null or empty list.");
    }
}

Now you can call your method: 现在你可以调用你的方法:

ThrowIf.Argument.IsNullOrEmpty<ParameterServiceReference.Parameter, List<ParameterServiceReference.Parameter>>(options.Parameters);

暂无
暂无

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

相关问题 该类型不能用作泛型类型或方法中的类型参数“TTo”。 没有隐式引用转换 - The type cannot be used as type parameter 'TTo' in the generic type or method. There is no implicit reference conversion 类型 &#39;&#39; 不能用作泛型类型或方法 &#39;&#39; 中的类型参数 &#39;T&#39;。 没有从 &#39;&#39; 到 &#39;&#39; 的隐式引用转换 - The type '' cannot be used as type parameter 'T' in the generic type or method ''. There is no implicit reference conversion from '' to '' 类型“int”不能用作泛型方法中的类型参数“T”。 'int' 没有拳击转换 - The type 'int' cannot be used as type parameter 'T' in the generic method. There is no boxing conversion from 'int' 类型&#39;&#39;不能用作通用类型或方法&#39;&#39;中的类型参数&#39;&#39;。 没有从“到”的隐式引用转换 - The type '' cannot be used as type parameter '' in the generic type or method ''. There is no implicit reference conversion from '' to '' 该类型不能在泛型类型或方法'BaseController <T>'中用作类型参数'T'。没有隐含的参考 - The type cannot be used as type parameter 'T' in the generic type or method 'BaseController<T>'. There is no implicit reference 错误:“该类型不能用作方法的通用类型中的类型参数。”这可以通过约束或强制转换来解决吗? - Error: “The type cannot be used as type parameter in the generic type of method.” Can this be solved with constraints or casting? CS0311 C# 该类型不能用作泛型类型或方法中的类型参数“TContext”。 EntityFrameworkCore - CS0311 C# The type cannot be used as type parameter 'TContext' in the generic type or method. EntityFrameworkCore 类型“ T”不能用作通用类型或方法中的类型参数“ T” - The type 'T' cannot be used as type parameter 'T' in the generic type or method 通用参数基类型:“没有从B到A的隐式引用转换” - Generic parameter base type: “There is no implicit reference conversion from B to A” 类型&#39;&#39;不能在通用类型或方法中用作类型参数&#39;T&#39; - The type '' cannot be used as type parameter 'T' in the generic type or method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM