不能通过引用转换、装箱转换、拆箱转换、包装转换或空类型转换来转换类型
Cannot convert type via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
在 C# 中,如果我有一个参数类型为接口的函数参数,如何传入实现该接口的对象。
这是一个例子:
一个函数的参数如下:
List<ICustomRequired>
我已有的名单如下:
List<CustomObject> exampleList
CustomObject
继承自ICustomRequired
接口
将 exampleList
作为参数传递的正确语法是什么?
这就是我想完成上述任务的方式:
exampleList as List<ICustomRequired>
但是我收到以下错误:
Cannot convert type via a reference conversion, boxing conversion,
unboxing conversion, wrapping conversion, or null type conversion
谢谢
您不能将一种类型的 List
转换为另一种类型的 List
。
如果你考虑一下,你会很高兴你不能。想象一下如果可能的话你可能造成的破坏:
interface ICustomRequired
{
}
class ImplementationOne : ICustomRequired
{
}
class ImplementationTwo: ICustomRequired
{
}
var listOne = new List<ImplementationOne>();
var castReference = listOne as List<ICustomRequired>();
// Because you did a cast, the two instances would point
// to the same in-memory object
// Now I can do this....
castReference.Add(new ImplementationTwo());
// listOne was constructed as a list of ImplementationOne objects,
// but I just managed to insert an object of a different type
但是请注意,这行代码是合法的:
exampleList as IEnumerable<ICustomRequired>;
这将是安全的,因为 IEnumerable
不会为您提供任何添加新对象的方法。
IEnumerable<T>
实际上定义为IEnumerable<out t>
,也就是说类型参数是Covariant.
你能把函数的参数改成IEnumerable<ICustomRequired>
吗?
否则,您唯一的选择就是创建一个新列表。
var newList = (exampleList as IEnumerable<ICustomRequired>).ToList();
或
var newList = exampleList.Cast<ICustomRequired>().ToList();
你不能这样做,你必须转换列表
exampleList.Cast<ICustomRequired>().ToList();
除了 List.Cast
,C# 的泛型还为 Covariance and contravariance 提供了很好的支持。这个例子使它按照我认为你最初想要的方式工作。
public class Program
{
public static void Main()
{
Foo(new List<Fim>());
}
public static void Foo<T>(List<T> bar) where T : IFim
{
throw new NotImplementedException();
}
public class IFim{}
public class Fim : IFim{}
}
在 C# 中,如果我有一个参数类型为接口的函数参数,如何传入实现该接口的对象。
这是一个例子:
一个函数的参数如下:
List<ICustomRequired>
我已有的名单如下:
List<CustomObject> exampleList
CustomObject
继承自ICustomRequired
接口
将 exampleList
作为参数传递的正确语法是什么?
这就是我想完成上述任务的方式:
exampleList as List<ICustomRequired>
但是我收到以下错误:
Cannot convert type via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
谢谢
您不能将一种类型的 List
转换为另一种类型的 List
。
如果你考虑一下,你会很高兴你不能。想象一下如果可能的话你可能造成的破坏:
interface ICustomRequired
{
}
class ImplementationOne : ICustomRequired
{
}
class ImplementationTwo: ICustomRequired
{
}
var listOne = new List<ImplementationOne>();
var castReference = listOne as List<ICustomRequired>();
// Because you did a cast, the two instances would point
// to the same in-memory object
// Now I can do this....
castReference.Add(new ImplementationTwo());
// listOne was constructed as a list of ImplementationOne objects,
// but I just managed to insert an object of a different type
但是请注意,这行代码是合法的:
exampleList as IEnumerable<ICustomRequired>;
这将是安全的,因为 IEnumerable
不会为您提供任何添加新对象的方法。
IEnumerable<T>
实际上定义为IEnumerable<out t>
,也就是说类型参数是Covariant.
你能把函数的参数改成IEnumerable<ICustomRequired>
吗?
否则,您唯一的选择就是创建一个新列表。
var newList = (exampleList as IEnumerable<ICustomRequired>).ToList();
或
var newList = exampleList.Cast<ICustomRequired>().ToList();
你不能这样做,你必须转换列表
exampleList.Cast<ICustomRequired>().ToList();
除了 List.Cast
,C# 的泛型还为 Covariance and contravariance 提供了很好的支持。这个例子使它按照我认为你最初想要的方式工作。
public class Program
{
public static void Main()
{
Foo(new List<Fim>());
}
public static void Foo<T>(List<T> bar) where T : IFim
{
throw new NotImplementedException();
}
public class IFim{}
public class Fim : IFim{}
}