0

I have a C# code of an assembly which fails to build in visual studio. After some research I found out all these errors are caused by <, > and $ symbols. Analyzing the assembly in .NET reflector turns out these parts of the code were created by the compiler. Here is a bit of code with these errors

    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;
}

Here i get two error for every <Register> for using the < and > symbols and three errors for every <$> for using <, $ and > symbols. What do you think might cause these errors?

user196569
  • 31
  • 1
  • 5
    Are you trying to recompile decompiled code? That won't work; the compiler can use name like those internally, but they're not allowed in your own code. You will need to rename these symbols. – Thomas Levesque Aug 09 '14 at 08:55
  • What's causing the errors? Source code that cannot ever compile. – david.pfx Aug 09 '14 at 10:28

1 Answers1

3

By the looks of things you are using an iterator block - a language featured introduced with C# 2.0.

Iterator blocks are syntactic sugar. When the compiler encounters an iterator block it will apply syntactic mapping which will expand the iterator block to become something more complicated internally. What you are looking at when you open your assembly in a disassembler is the product of syntactic mapping.

The compiler does not want you to be able to refer to this compiler generated code (as it would be confusing and potentially dangerous/conflicting) so it gives the generated identifiers unspeakable names that would otherwise be invalid in C#.

In summary, the <, > and $ symbols are invalid in C# but not in IL (which is what the disassembler will best-attempt to accurately reconstruct the source code from).

User 12345678
  • 7,714
  • 2
  • 28
  • 46
  • So what would be the best solution of this problem in your opinion? – user196569 Aug 09 '14 at 10:16
  • reading you link about iterator block leads me to think there is a way to turn my iterator box back into it's original form, into a method. i wonder how can i do that – user196569 Aug 09 '14 at 13:23
  • @user196569 That article in particular goes into far more detail than I could in an answer here. You could ask a different question about that. – User 12345678 Aug 09 '14 at 13:49