【问题标题】:Failed to compile code due to <, > and $ symbols由于 <、> 和 $ 符号而无法编译代码
【发布时间】:2014-08-09 08:52:23
【问题描述】:

我有一个无法在 Visual Studio 中构建的程序集的 C# 代码。经过一番研究,我发现所有这些错误都是由 和 $ 符号引起的。分析 .NET 反射器中的程序集发现这些代码部分是由编译器创建的。这是一些带有这些错误的代码

    private System.Collections.IEnumerator Register(string name, string password, string password2, string email)
{
    LoginFengKAI.<Register>c__Iterator2 <Register>c__Iterator = new LoginFengKAI.<Register>c__Iterator2();
    <Register>c__Iterator.name = name;
    <Register>c__Iterator.password = password;
    <Register>c__Iterator.password2 = password2;
    <Register>c__Iterator.email = email;
    <Register>c__Iterator.<$>name = name;
    <Register>c__Iterator.<$>password = password;
    <Register>c__Iterator.<$>password2 = password2;
    <Register>c__Iterator.<$>email = email;
    <Register>c__Iterator.<>f__this = this;
    return <Register>c__Iterator;
}

这里我得到两个错误,每个&lt;Register&gt; 使用 符号,每个 &lt;$&gt; 使用 符号三个错误。 您认为什么可能导致这些错误?

【问题讨论】:

  • 您是否要重新编译反编译的代码?那是行不通的;编译器可以在内部使用类似的名称,但在您自己的代码中不允许使用它们。您需要重命名这些符号。
  • 是什么导致了错误?永远无法编译的源代码。

标签: c# visual-studio compiler-errors compiler-generated


【解决方案1】:

从外观上看,您使用的是 iterator block - C# 2.0 引入的一种语言。

迭代器块是syntactic sugar。当编译器遇到迭代器块时,它会应用句法映射,这将扩展迭代器块,使其内部变得更复杂。当您在反汇编程序中打开程序集时,您看到的是句法映射的产物。

编译器不希望您能够引用此编译器生成的代码(因为它会令人困惑并具有潜在的危险/冲突),因此它为生成的标识符提供了不可描述的名称,否则这些名称在 C# 中将是无效的。

总之, 和 $ 符号 在 C# 中无效,但在 IL 中无效(这是反汇编程序最好的方法-尝试准确地重构源代码代码来自)。

【讨论】:

  • 那么您认为这个问题的最佳解决方案是什么?
  • 阅读你关于迭代器块的链接让我想到有一种方法可以将我的迭代器框变回原来的形式,变成一种方法。我想知道我该怎么做
  • @user196569 那篇文章的详细程度比我在这里的回答要详细得多。你可以问一个不同的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
  • 2023-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多