对象类型末尾的&符号是什么?

gan*_*ers 12 c# reflector decompiling

我不得不重新编译一些代码,我不知道这个语法是什么?你们可以帮忙,还是指点一下这是什么意思?我用Google搜索并搜索了这个网站,找不到任何东西.

只需一行代码:

Rectangle pageBounds;
// ISSUE: explicit reference operation
// ISSUE: variable of a reference type
Rectangle& local = @pageBounds;
Run Code Online (Sandbox Code Playgroud)

@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;
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*eld 21

在该行代码之前的注释正在告诉您究竟发生了什么.类型名称后面的&符号表示它是引用类型,变量名称前面的@生成对该变量的引用.

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

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

例如,当您使用refout参数时,隐式地创建对局部变量的引用,但使用关键字而不是显式键入参数作为参考.(例如,如果你有一个out int x,即内部变量的类型的Int32&.)的意图的代码,如果它是合法的C#,将是pageBoundslocal人的同一个实例有两个不同的名字; 你做的任何事都发生在另一个.所以,例如,这个非法代码:

Rectangle pageBounds;
Rectangle& local = @pageBounds;
local = new Rectangle();
Run Code Online (Sandbox Code Playgroud)

与此法律代码相同:

Rectangle pageBounds = new Rectangle();
Run Code Online (Sandbox Code Playgroud)

如果你试图编译as-decompiled的代码,你会得到一个错误,因为编译器将&视为按位,并且会抱怨你使用了一个类型,好像它是一个变量.但这没关系,因为你没有从C#源文件中获取它.你反编译了一个IL方法来获取它,你可以在IL中做很多在C#中非法的事情.当您反编译代码时,这种情况一直发生; 你会看到非法的类和方法名称.它只是意味着,在原有基础上的代码,并没有转化直接返回到C#编译器生成的IL,但表现你想要的方式.您要获取的代码很简单,反编译器最好尝试从它生成的IL生成C#代码.

您可以在众多关于它们的Jetbrains错误报告中看到产生这些引用的代码类型的示例: