14

我不得不反编译一些代码,我不知道这个语法是什么?你们能帮忙吗,或者指点我写一篇关于它是什么的文章?我用谷歌搜索并搜索了这个网站,但找不到任何东西。

只需一行代码:

Rectangle pageBounds;
// ISSUE: explicit reference operation
// ISSUE: variable of a reference type
Rectangle& local = @pageBounds;

@Rectangle 对象类型末尾和变量@之前的符号是什么?pageBounds

这是我需要修复的最后一行代码,以便让这个可执行文件再次编译。

这是使用这种语法的方法,我可以删除它吗?

protected override void OnPrintPage(PrintPageEventArgs e)
{
  Application.DoEvents();
  ++this._pageNum;
  float num1;
  if (this.Header != null)
  {
    num1 = this.Header.CalculateHeight(this, e.Graphics);
    this.Header.Draw(this, (float) e.MarginBounds.Top, e.Graphics, e.MarginBounds);
  }
  else
    num1 = 0.0f;
  float num2;
  if (this.Footer != null)
  {
    num2 = this.Footer.CalculateHeight(this, e.Graphics);
    this.Footer.Draw(this, (float) e.MarginBounds.Bottom - num2, e.Graphics, e.MarginBounds);
  }
  else
    num2 = 0.0f;
  Rectangle pageBounds;
  // ISSUE: explicit reference operation
  // ISSUE: variable of a reference type
  Rectangle& local = @pageBounds;
  int left = e.MarginBounds.Left;
  Rectangle marginBounds = e.MarginBounds;
  int y = (int) ((double) marginBounds.Top + (double) num1);
  marginBounds = e.MarginBounds;
  int width = marginBounds.Width;
  marginBounds = e.MarginBounds;
  int height = (int) ((double) marginBounds.Height - (double) num2 - (double) num1);
  // ISSUE: explicit reference operation
  local = new Rectangle(left, y, width, height);
  float yPos = (float) pageBounds.Top;
  bool flag = false;
  int num3 = 0;
  while (this._printIndex < this._printElements.Count)
  {
    PrintElement printElement = (PrintElement) this._printElements[this._printIndex];
    float num4 = printElement.CalculateHeight(this, e.Graphics);
    if ((double) yPos + (double) num4 > (double) pageBounds.Bottom && num3 != 0)
    {
      flag = true;
      break;
    }
    else
    {
      printElement.Draw(this, yPos, e.Graphics, pageBounds);
      yPos += num4;
      ++this._printIndex;
      ++num3;
    }
  }
  e.HasMorePages = flag;
}
4

2 回答 2

24

那行代码之前的注释告诉你到底发生了什么。类型名称后的 & 符号表示它是一个引用类型,变量名称前的 @ 生成对该变量的引用。

(@ 符号也可以在 C# 代码中用于“转义”关键字以用作变量名,但这不是这里发生的事情。“pageBounds”不是 C# 关键字。)

请注意,这不是有效的 C# 语法——您不能在 C# 中引用局部变量,尽管 CLR 支持它。(注意:从 C# 7.0 开始,这不再是真的;这里描述了语法,但它不使用 ,&所以这个反编译的代码仍然是无效的 C#)。

例如,当您使用refout参数时,会隐式创建对局部变量的引用,但使用关键字而不是显式键入参数作为引用。(例如,如果您有一个out int x,则该变量在内部的类型为Int32&。)如果它是合法的 C#,代码的意图pageBounds就是这样,并且local是具有两个不同名称的同一个实例;你对一个人所做的任何事情都会发生在另一个人身上。因此,例如,这个非法代码:

Rectangle pageBounds;
Rectangle& local = @pageBounds;
local = new Rectangle();

将与此法律代码相同:

Rectangle pageBounds = new Rectangle();

如果你试图编译反编译的代码,你会得到一个错误,因为编译器将 & 视为按位与,并且会抱怨你使用了一个类型,就好像它是一个变量一样。但这没关系,因为您不是从 C# 源文件中获取它的。你反编译了一个 IL 方法来得到它,你可以在 IL 中做很多在 C# 中是非法的事情。当您反编译代码时,这种情况总是会发生;例如,您会看到非法的类和方法名称。这只是意味着编译器基于原始代码生成 IL,该代码不会直接转换回 C#,而是按照您想要的方式运行。你得到的代码很简单,反编译器从它拥有的 IL 生成 C# 代码的最佳尝试。

您可以在大量 Jetbrains 错误报告中看到生成这些引用的代码示例:

于 2012-06-16T22:49:58.980 回答
3

请参阅此处 - http://msdn.microsoft.com/en-us/library/aa664670(v=vs.71).aspx(虽然从未使用过)

The prefix "@" enables the use of keywords as identifiers,这在与其他编程语言交互时很有用。字符 @ 实际上不是标识符的一部分,因此标识符在其他语言中可能被视为普通标识符,没有前缀。带有@ 前缀的标识符称为逐字标识符。允许对不是关键字的标识符使用 @ 前缀,but strongly discouraged as a matter of style.

这个例子:

class @class
{
   public static void @static(bool @bool) {
      if (@bool)
         System.Console.WriteLine("true");
      else
         System.Console.WriteLine("false");
   }   
}
class Class1
{
   static void M() {
      cl\u0061ss.st\u0061tic(true);
   }
}
于 2012-06-16T22:11:48.670 回答