[英]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
,其中T
是List
:
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.