c# - 由于<,>和$符号而导致无法编译代码

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

我有一个无法在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;
}

在这里,对于使用<和>符号的每个<Register>,我都会得到两个错误;对于使用<,$和>符号的每个<$>,我都会得到三个错误。
您认为可能导致这些错误的原因是什么?

最佳答案

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

迭代器块为syntactic sugar。当编译器遇到迭代器块时,它将应用语法映射,这将使迭代器块扩展为内部更复杂的东西。在反汇编程序中打开装配时,您正在查看的是语法映射的产品。

编译器不希望您能够引用此编译器生成的代码(因为这会造成困惑,并可能导致危险/冲突),因此它为生成的标识符提供了难以理解的名称,否则它们在C#中无效。

总而言之,<,>和$符号在C#中无效,但在IL中无效(这是反汇编程序最好地尝试从其正确重构源代码的方式)。

关于c# - 由于<,>和$符号而导致无法编译代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25216852/

相关文章:

c# - If 语句在 C# 中不起作用

javascript - 通过 ajax 调用向序列化表单添加额外数据

c# - 应用程序之间的对象共享?

JavaScript Intellisense 在 Visual Studio 2015 中不工作

java - 如何对变量使用 unicode 转义序列?

scala - 消除重载方法定义的歧义

c# - Data Transfer Object 中的属性应该扩展外键还是简单地公开它们的主键

c++ - 这个局部变量是否隐藏/隐藏了另一个常态或 Visual Studio 中的错误?

c# - 从解决方案中的所有文件中删除注释

compiler-errors - Fortran90 未分类语句 While 循环