操作可能破坏运行? [英] Operation could destabilize the runtime?

查看:224
本文介绍了操作可能破坏运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦理解问题是什么在这里一点点。我有一点code,拉记录使用LINQ的数据库,并将它们放入被浇铸成一个接口的对象。它看起来有点像这样:

I'm having a little bit of trouble understanding what the problem is here. I have a bit of code that pulls records from a database using LINQ and puts them into an object which is cast into an interface. It looks a bit like this:

public IEnumerable<ISomeObject> query()
{
    return from a in dc.SomeTable
           select new SomeObject
           {
             //Assign various members here
           } as ISomeObject;
}

当我测试,我把返回的IEnumerable到一个变量调用结果和运行这一行:

When I test this, I put the returned IEnumerable into a variable called results and run this line:

Assert.AreEqual(EXPECTED_COUNT, results.Count());

在此运行时,我得到一个System.Security.VerificationException:操作可能破坏运行时

When this is run, I get a System.Security.VerificationException: "Operation could destabilize the runtime."

我找到了解决办法<一href="http://devlicio.us/blogs/derik_whittaker/archive/2008/11/29/quot-operation-could-destabilize-the-runtime-quot-from-casting-from-concrete-to-interfaces-with-linq.aspx">here,这是这样的:

I found the solution here, which is this:

var results = from a in dc.SomeTable
              select new SomeObject
              {
                //Assign various members here
              } as ISomeTable;
return results.OfType<ISomeObject>();

这工作,但我无法理解这里发生了什么。为什么我得到的异常摆在首位,又是如何的code线上面的解决? MSDN文档似乎表明,这是类型安全的问题,但我没有看到那里的previous code是类型不安全的。

This works, but I'm having trouble understanding what's happening here. Why did I get the exception in the first place and how did the lines of code above fix it? The MSDN documentation seems to suggest that this is an issue of type safety, but I'm not seeing where the previous code was type-unsafe.

更新 多一点点信息,我发现了。第一个例子的作品,如果我做的返回类型的IQueryable。这揭示了一点点光的什么的打算错了,但我仍然感到困惑的为什么的。为什么没有编译器逼我投了IEnumerable成一个IQueryable?

UPDATE A little bit more information I found out. The first example works if I make the return type IQueryable. This sheds a little bit more light on what was going wrong, but I'm still confused about the why. Why didn't the compiler force me to cast the IEnumerable into an IQueryable?

推荐答案

我相信这是协方差或逆变的问题所指出的的这个论坛帖子

I believe it is an issue of covariance or contravariance as noted by this forum post.

请参阅<一href="http://blogs.msdn.com/ericlippert/archive/2007/10/17/covariance-and-contravariance-in-c-part-two-array-covariance.aspx">Covariance和逆变在C#中,第二部分:数组协变和的其余<一href="http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx">Covariance和逆变系列在埃里克利珀的博客。

See Covariance and Contravariance in C#, Part Two: Array Covariance and the rest of the Covariance and Contravariance series at Eric Lippert's blog.

虽然他正在处理中我挂了文章阵列,相信类似的问题presents本身在这里。有了您的第一个例子,你是返回一个的IEnumerable 可能包含实现一个接口,对象的的比 ISomeTable (即 - 你可以把海龟到动物的IEnumerable时的IEnumerable只能包含长颈鹿)。我认为当你返回的IQueryable 是因为这是它的工作原理的原因的放大/更宽的比什么都重要,你可以退货,所以你保证什么你回来,你将能够处理(?)。

Although he is dealing with Arrays in the article I linked, I believe a similar problem presents itself here. With your first example, you are returning an IEnumerable that could contain objects that implement an interface that is larger than ISomeTable (i.e. - you could put a Turtle into an Animals IEnumerable when that IEnumerable can only contain Giraffes). I think the reason it works when you return IQueryable is because that is larger/wider than anything you could return, so you're guaranteed that what you return you will be able to handle(?).

在第二个例子中, OfType 是保证什么获取返回的是一个对象,商店所有必要的信息,仅返回那些可以转换为长颈鹿元素。

In the second example, OfType is ensuring that what gets returned is an object that stores all the information necessary to return only those elements that can be cast to Giraffe.

我是pretty的确保它是与类型安全的上述问题,但作为埃里克利珀说<一href="http://blogs.msdn.com/ericlippert/archive/2007/10/24/covariance-and-contravariance-in-c-part-five-higher-order-functions-hurt-my-brain.aspx">Higher高阶函数伤害了我的大脑和我有麻烦EX pressing precisely为什么这是一个合作/逆变问题。

I'm pretty sure it has something to do with the issues of type safety outlined above, but as Eric Lippert says Higher Order Functions Hurt My Brain and I am having trouble expressing precisely why this is a co/contravariant issue.

这篇关于操作可能破坏运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆