C# 中的 AddressOf 替代方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/280953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:27:34  来源:igfitidea点击:

AddressOf alternative in C#

c#address-operator

提问by

Could anyboby help me with the alternative solution in C# regarding AddressOf operator in VB6? AddressOf returns a long value. What way can I get the output in C#?

任何人都可以帮助我使用 C# 中有关VB6 中 AddressOf 运算符的替代解决方案吗?AddressOf 返回一个长值。我可以通过什么方式获得 C# 中的输出?

回答by Joel Coehoorn

C# has special syntax for assigning functions to delegates/events. Just use <delegate> += <function>;

C# 具有用于将函数分配给委托/事件的特殊语法。只需使用<delegate> += <function>;

If you're actually trying to get the address for some other use, you're out of luck. One of the things about managed code is that these addresses aren't necessarily fixed. While it would be unlikely that most functions would ever change there are circumstances where it's possible.

如果您实际上是想将地址用于其他用途,那么您就不走运了。关于托管代码的一件事是这些地址不一定是固定的。虽然大多数功能不太可能改变,但在某些情况下是可能的。

回答by Harper Shelby

Apparently, this can be done (though I'm not sure where you'd need it). Here's the MSDN page.

显然,这是可以做到的(虽然我不确定你在哪里需要它)。这是MSDN 页面

回答by TcKs

EventHandler handler1 = this.button1_Click;
EventHandler handler2 = new EventHandler( this.button1_Click );
...
...
...
void button1_Click( object sender, EventArgs e ){
    // ....
}

Both notation are equivalent.

两种表示法是等价的。

回答by John Rudy

Expanding on Harper Shelby's answer, yes it can be done, but it's generally a code smell to do so in .NET.

扩展 Harper Shelby 的回答,是的,它可以做到,但在 .NET 中这样做通常是一种代码味道。

To get the address of a variable in C#, you can use C-style pointer (*) /address (&) / dereference (->) syntax. In order to do this, you will have to compile the app with the /unsafe compiler switch, as you're bouncing out of the safety net of managed code as soon as you start dealing with memory addresses directly.

要在 C# 中获取变量的地址,可以使用 C 风格的指针 (*) / 地址 (&) / 取消引用 (->) 语法。为此,您必须使用 /unsafe 编译器开关编译应用程序,因为一旦开始直接处理内存地址,您就会跳出托管代码的安全网。

The sample from MSDN tells most of the story:

来自 MSDN 的示例讲述了大部分故事:

int number;
int* p = &number;
Console.WriteLine("Value pointed to by p: {0}", p->ToString());

This assigns the address of the numbervariable to the pointer-to-an-int p.

这将number变量的地址分配给指向 an-int 的指针p

There are some catches to this:

这有一些问题:

  1. The variable whose address you are fetching must be initialized. Not a problem for value types, which default, but it is an issue for reference types.
  2. In .NET, variables can move in memory without you being aware of it. If you need to deal with the address of a variable, you really want to use fixedto pin the variable in RAM.
  3. & can only be applied to a variable, not a constant nor a value. (In other words, you cannot use a construct like int* p = &GetSomeInt();)
  4. Again, your code must be compiled in unsafe mode, which flags the CLR that you will be using features outside the managed code "safety net."
  1. 您要获取其地址的变量必须被初始化。对于默认值类型来说不是问题,但对于引用类型来说是一个问题。
  2. 在 .NET 中,变量可以在您不知道的情况下在内存中移动。如果你需要处理一个变量的地址,你真的想使用fixed将变量固定在 RAM 中。
  3. & 只能应用于变量,不能应用于常量或值。(换句话说,你不能使用像这样的构造int* p = &GetSomeInt();
  4. 同样,您的代码必须在不安全模式下编译,这会标记 CLR 您将使用托管代码“安全网”之外的功能。

Generally, my advice in this world is to seriously consider whyyou think you need to do this in the .NET world. One of .NET's missions was to shield developers from going against the metal, and this feature is counter to that mission. It exists for those (rare) scenarios where it is needed; if you find yourself frivolously using this simply because you can, you're probably mis-using it and introducing code smell.

一般来说,我在这个世界的建议是认真考虑为什么你认为你需要在 .NET 世界中这样做。.NET 的使命之一是保护开发人员免受金属的侵害,而此功能与该使命背道而驰。它存在于需要它的那些(罕见)场景中;如果您发现自己只是因为可以而轻率地使用它,那么您可能误用了它并引入了代码异味。

Avoid it if possible, but know how to use it if you absolutely must.

如果可能,请避免使用它,但如果绝对必须要知道如何使用它。