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