将接口数组转换为结构数组时隐式转换无效
我有一个实现一些接口
的struct
。在我拥有一个 struct 实现的数组并尝试将该数组隐式转换为另一个 interface 类型的数组之前,这种方法可以正常工作。 (请参阅下面的代码示例)
using System.Collections.Generic;
namespace MainNS
{
public interface IStructInterface
{
string Name { get; }
}
public struct StructImplementation : IStructInterface
{
public string Name
{
get { return "Test"; }
}
}
public class MainClass
{
public static void Main()
{
StructImplementation[] structCollection = new StructImplementation[1]
{
new StructImplementation()
};
// Perform an implicit cast
IEnumerable<IStructInterface> castCollection = structCollection; // Invalid implicit cast
}
}
}
编译上述代码时,出现错误:
错误CS0029:无法将类型“MainNS.StructImplementation[]”隐式转换为“MainNS.IStructInterface[]”
如果我将StructImplementation
更改为class
我没有问题,所以我假设我想做的事情要么无效;或者我是盲目的并且错过了一些明显的东西。
对此的任何建议或解释将不胜感激。
编辑
如果其他人遇到此问题并且使用不同的方法不太理想(就像我的情况一样),我使用 LINQ 方法 Cast
。因此,在上面的示例中,我将使用以下内容执行转换:
IEnumerable<IStructInterface> castCollection = structCollection.Cast<IStructInterface>();
MSDN 上有一篇关于 通用类型的变化,我发现这非常有用。
I have a struct
that implements some interface
. This works fine until I have an array of the struct
implementation and try to implicitly cast that array to another array of the interface
type. (See the below code example)
using System.Collections.Generic;
namespace MainNS
{
public interface IStructInterface
{
string Name { get; }
}
public struct StructImplementation : IStructInterface
{
public string Name
{
get { return "Test"; }
}
}
public class MainClass
{
public static void Main()
{
StructImplementation[] structCollection = new StructImplementation[1]
{
new StructImplementation()
};
// Perform an implicit cast
IEnumerable<IStructInterface> castCollection = structCollection; // Invalid implicit cast
}
}
}
When compiling the above code, I get the error:
error CS0029: Cannot implicitly convert type 'MainNS.StructImplementation[]' to 'MainNS.IStructInterface[]'
If I change StructImplementation
to a class
I have no problems, so I'm assuming what I'm trying to do is either not valid; or I'm being blind and missing something obvious.
Any advice or explanation for this would be appreciated.
EDIT
In case anyone else has this issue and using a different approach is less than ideal (as was the case in my situation), I worked around my issue using the LINQ method Cast<T>()
. So in the example above, I would perform the cast using something like:
IEnumerable<IStructInterface> castCollection = structCollection.Cast<IStructInterface>();
There is a good article on MSDN about Variance in Generic Types, which I found very useful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数组方差仅允许引用保留情况,因此仅适用于类。它本质上是将原始数据视为对不同类型的引用。这对于结构来说是不可能的。
Array variance ony allows for the reference preserving case, and so only works for classes. It is essentially treating the original data as a reference to a different type. This is simply not possible with structs.
结构不支持您在此处使用的协变,因为它们是值类型而不是引用类型。请参阅此处了解更多信息。
Convariance, which you are using here, is not supported for structs, because they are value types and not reference types. See here for a little bit more info.