Empty <> before C# class name

做~自己de王妃 提交于 2019-12-13 01:12:22

问题


I need to repair C# code that was decompiled on with .NET 4. Unfortunately, on my workstation there is only .NET 3.5 installed and that might be the reason why the following code won't compile:

ReportPage.<>c__DisplayClass26 <>c__DisplayClass = new ReportPage.<>c__DisplayClass26();

What <> construct means here? There are no c__DisplayClass26 strings anywhere in the code except this line. This might mean that this name is constructed from some metainformation that was missed during decompilation.


回答1:


The C# compiler sometime has to introduce variables/fields/classes. When he has to do it, he prepends them <> to their name, so there won't be a name collision (with the C# compiler, it's illegal to name something with those two characters). This happens for example for auto generated properties, the yield keyword, lambda & anonymous functions/delegates, the new async keyword (the one that is introduced in the Async CTP and that will probably be present in C# 5.0)...

Now... It's strange that you have the c__DisplayClass26 only there. Perhaps your decompiler wasn't up to the par. Try looking at the code with IlSpy.

Look at this pastebin: http://pastebin.com/pTRVyVdp (it isn't mine). There is an example with c__DisplayClass. In the first half of the text there is the "original" code, in the second half the decompile one. You see, in this case it's used for a closure. At lines 32/33 there is a [CompilerGenerated] private sealed class <>c__DisplayClass2. You should have it too in your code.

Mmmh... IlSpy is a little too much good :-) Often it's able to reconstruct the "original" code from the CompilerGenerated code.




回答2:


xanatos is correct; you are looking at a class that was automatically generated to implement closure semantics.

My handy guide to interpreting the magic names that the C# compiler generates when it has to make something on your behalf is here:

Where to learn about VS debugger 'magic names'

But again let me emphasize that you should not rely upon this; this is for entertainment purposes only. We reserve the right to change this scheme at any time.




回答3:


This was a compiler-generated class. It was most likely created from an anonymous delegate or lambda inside of a method of the ReportPage class.

You should be able to see where this is used in your decompiled code, and try to figure out what it's doing to back it out.



来源:https://stackoverflow.com/questions/7357525/empty-before-c-sharp-class-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!