C# 4.0 - 在动态对象上调用受保护方法是否会调用 TryInvokeMember()?

发布于 2024-08-24 02:16:15 字数 466 浏览 6 评论 0原文

在 C# 4.0 中,有一个新的 DynamicObject。

它提供了一个“神奇方法”TryInvokeMember(),当尝试调用不存在的方法时会调用该方法。

http://msdn。 microsoft.com/en-us/library/system.dynamic.dynamicobject.tryinvokemember%28VS.100%29.aspx

我想知道的是尝试调用受保护方法时是否调用 TryInvokeMember()在定义类之外。

我将这种行为与 PHP 进行了对比,在这种情况下,PHP 确实调用了其等效的“魔术方法”__call()。

In C# 4.0, there is a new DynamicObject.

It provides a "magic method" TryInvokeMember() that gets called when trying to call a method that does not exist.

http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.tryinvokemember%28VS.100%29.aspx

What I would like to know is if TryInvokeMember() gets called when trying to call a protected method from outside the defining class.

I am contrasting the behaviour with PHP, which does call its equivalent "magic method" __call() in this situation.

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

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

发布评论

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

评论(1

瀞厅☆埖开 2024-08-31 02:16:15

当您编写的调用将调用不可访问的方法(使用标准 C# 访问规则)时,将不会调用不可访问的方法,并且运行时将调用 TryInvokeMember (您可以在其中以其他方式处理呼叫)。下面是一个示例,您可以尝试一下:

class Test : DynamicObject {
  public void Foo() {
    Console.WriteLine("Foo called");
  }
  protected void Bar() {
    Console.WriteLine("Bar called");
  }

  public override bool TryInvokeMember
      (InvokeMemberBinder binder, object[] args, out object result) {
    Console.WriteLine("Calling: " + binder.Name);
    return base.TryInvokeMember(binder, args, out result);
  }
}

现在,我们可以创建该对象的一个​​实例并尝试调用它的一些方法:

dynamic d = new Test();
d.Foo(); // this will call 'Foo' directly (without calling 'TryInvokeMember')
d.Bar(); // this will call 'TryInvokeMember' and then throw exception

因此,如果您调用 TryInvokeMember 的 base 实现,当调用不可访问的方法时,C# 动态绑定器将失败,但您可以在 TryInvokeMember 中定义自己的处理方式(通过将 result 设置为某个值)值并返回true)。

When you write a call that would invoke a method that is not accessible (using the standard C# access rules), then the inaccessible method won't be called and the runtime will call the TryInvokeMember (where you can handle the call in some other way). Here is an example, so that you can try it:

class Test : DynamicObject {
  public void Foo() {
    Console.WriteLine("Foo called");
  }
  protected void Bar() {
    Console.WriteLine("Bar called");
  }

  public override bool TryInvokeMember
      (InvokeMemberBinder binder, object[] args, out object result) {
    Console.WriteLine("Calling: " + binder.Name);
    return base.TryInvokeMember(binder, args, out result);
  }
}

Now, we can create an instance of the object and try calling some of its methods:

dynamic d = new Test();
d.Foo(); // this will call 'Foo' directly (without calling 'TryInvokeMember')
d.Bar(); // this will call 'TryInvokeMember' and then throw exception

So, if you call the base implementation of TryInvokeMember, the C# dynamic binder will fail when calling an inaccessible method, but you can define your own handling of the case in TryInvokeMember (by setting the result to some value and returning true).

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