C# 中的 AddressOf 替代方案

发布于 2024-07-08 21:24:39 字数 111 浏览 5 评论 0原文

Anyboby 可以帮我找到有关 VB6 中的 AddressOf 运算符 的 C# 替代解决方案吗? AddressOf 返回一个长整型值。 我怎样才能得到C#的输出?

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#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

七婞 2024-07-15 21:24:40

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

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

C# has special syntax for assigning functions to delegates/events. Just use <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.

野心澎湃 2024-07-15 21:24:40

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

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

书信已泛黄 2024-07-15 21:24:40
EventHandler handler1 = this.button1_Click;
EventHandler handler2 = new EventHandler( this.button1_Click );
...
...
...
void button1_Click( object sender, EventArgs e ){
    // ....
}

两种表示法是等效的。

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

Both notation are equivalent.

云裳 2024-07-15 21:24:39

扩展 Harper Shelby 的答案,是的,可以做到,但在 .NET 中这样做通常会产生代码味道。

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

MSDN 中的示例讲述了大部分内容:

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

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

对此有一些注意事项:

  1. 您要获取其地址的变量必须进行初始化。 对于默认的值类型来说不是问题,但对于引用类型来说这是一个问题。
  2. 在 .NET 中,变量可以在您不知情的情况下在内存中移动。 如果您需要处理变量的地址,您确实需要使用 已修复以将变量固定在 RAM 中。
  3. & 只能应用于变量,不能应用于常量或值。 (换句话说,您不能使用像 int* p = &GetSomeInt(); 这样的构造)
  4. 同样,您的代码必须在不安全模式下编译,这会向 CLR 标记您将使用的功能在托管代码“安全网”之外。

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

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

Expanding on Harper Shelby's answer, yes it can be done, but it's generally a code smell to do so in .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.

The sample from MSDN tells most of the story:

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

This assigns the address of the number variable to the pointer-to-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 fixed to 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."

Generally, my advice in this world is to seriously consider why you 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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文