【问题标题】:Why does simple array and Linq generate VerificationException: Operation could destabilize the runtime为什么简单数组和 Linq 会生成 VerificationException:操作可能会破坏运行时
【发布时间】:2009-01-25 22:23:56
【问题描述】:

C# 中一个非常简单的 ?: 运算符,结合一个简单的数组和对 Reverse() 的 LINQ(对象)调用显然足以导致异常“System.Security.VerificationException:操作可能破坏运行时”。在以“高”信任运行的 ASP.NET 网站上运行时(请注意,“完整”是默认设置)

这是代码,以及我想出的简单解决方法:

protected void Page_Load(object sender, EventArgs e) {
    Repro();
    //Workaround();
}

private IEnumerable<string> Repro() {
    bool test = true;
    string[] widgets = new string[0];
    return test ? widgets : widgets.Reverse();
}

private IEnumerable<string> Workaround() {
    bool test = true;
    string[] widgets = new string[0];
    if (test) {
        return widgets;
    } else {
        return widgets.Reverse();
    }
}

要将信任级别设置为“高”,必须将以下内容添加到 web.config 文件中:

<trust level="High" originUrl=""/>

解决方法是功能等同于有问题的 ?: 操作符来重现问题。我从阅读相关文章的猜测是这是一个 C# 编译器错误。有人知道这里发生了什么吗?

【问题讨论】:

    标签: c# asp.net linq linq-to-objects medium-trust


    【解决方案1】:

    这一行没有问题:

    return test ? widgets.AsEnumerable() : widgets.Reverse();
    

    【讨论】:

      【解决方案2】:

      我发现更改表达式的顺序也可以解决这个问题:

      private IEnumerable<string> Workaround() {
          bool test = true;
          string[] widgets = new string[0];
          return !test ? widgets.Reverse() : widgets;
      }
      

      去看看!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多