Getting a weird character when decompiling source code

124 views Asked by At

When attempting to decompile an application I'm getting a rectangular box. I'm not sure if there is a way to interpret what this is or not.

 public void Add(MessageErrorCode errorCode);

 public void Add(SegmentErrorContext segmentContext);

 public void AddRange(IEnumerable<MessageErrorCode> errorCodes);

 public void AddRange(IEnumerable<SegmentErrorContext> segmentContexts);

 [IteratorStateMachine(typeof())]
 public IEnumerable<string> Flatten();

 public void Sort();
3

There are 3 answers

0
NineBerry On BEST ANSWER

The IteratorStateMachine attribute is added to the generated IL machine code by the compiler for "iterator" methods that use yield internally.

You can just remove that attribute for the code to work.

For illustration look at this SharpLab gist: https://sharplab.io/#gist:fc2cc09bb9cf72774eb724d655613cbf

The code

using System;
using System.Collections.Generic;

public class C 
{
    public IEnumerable<int> M() 
    {
        yield return 0;
        yield return 1;
    }
}

is translated into IL Machine Code like this:

[IteratorStateMachine(typeof(<M>d__0))]
public IEnumerable<int> M()
{
    <M>d__0 <M>d__ = new <M>d__0(-2);
    <M>d__.<>4__this = this;
    return <M>d__;
}
0
Dawnkeeper On

It's added by the compiler.

You shouldn't apply the IteratorStateMachine attribute to methods in your code. For methods in Visual Basic that have the Iterator modifier, the compiler will apply the IteratorStateMachine attribute in the IL that it emits

From the Microsoft Docs.

3
Olivier Jacot-Descombes On

C# has strict Naming rules for identifiers. The linked documentation says:

  • Identifiers must start with a letter or underscore (_).
  • Identifiers may contain Unicode letter characters, decimal digit characters, Unicode connecting characters, Unicode combining characters, or Unicode formatting characters. For more information on Unicode categories, see the Unicode Category Database.

Declared Element Names (Visual Basic) are similar. The following is true for VB as well.

The C# compiler converts the C# code text into IL. IL allows about any kind of identifier. Especially it can contain special characters not considered as letters, digits or an underscore. ECMA 335, 5th edition, Partition II, Section 5.3 (PDF page 135) states:

  • ILAsm syntax allows the use of any identifier that can be formed using the Unicode character set (see Partition I). To achieve this, an identifier shall be placed within single quotation marks.

For compiler generated objects (methods, classes, etc.) creates identifiers that are not valid C# identifiers to avoid conflicts with identifiers existing in your code.

So, don't be surprised if decompiled code (form IL to C#) contains weird identifiers.