处理未在动态对象上定义的C#方法(也称为respond_to / method_missing) [英] Handling a C# method that isn't defined on a dynamic object (aka respond_to/method_missing)

查看:106
本文介绍了处理未在动态对象上定义的C#方法(也称为respond_to / method_missing)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到C#4中的新的动态支持,是否可以以一种方式编写一个类,如果一个方法在一个实例上被调用,那个方法不存在,那么将派遣传递给另一个方法?这可能看起来像:

  public class Apple:... {
// ...

private ... MethodMissing(string name,...){
if(name ==TurnIntoOrange){
// do something
}
}
}

dynamic d = new Apple();
d.TurnIntoOrange(); //苹果实际上没有定义;将传递给MethodMissing。

其他语言将称为method_missing支持,位于元编程的更一般标题下。我不知道什么C#调用这个具体。但是可以吗?

解决方案

绝对。执行 IDynamicMetaObjectProvider 或派生自 DynamicObject 。有关一些很好的例子,请参阅 DLR文档。 p>

以下是 DynamicObject 的简单示例:

 使用系统; 
使用System.Dynamic;

public class MyDynamic:DynamicObject
{
public override bool TryInvokeMember
(InvokeMemberBinder binder,
object [] args,
out object result )
{
Console.WriteLine(我将调用:{0},
binder.Name);
result =dummy;
返回true;
}

public string NormalMethod()
{
Console.WriteLine(In NormalMethod);
returnnormal;
}
}

class Test
{
static void Main()
{
dynamic d = new MyDynamic() ;
Console.WriteLine(d.HelloWorld());
Console.WriteLine(d.NormalMethod());
}
}

插件>



我有一个更大的例子, DynamicObject 深度C#的第二版,但是我还没有实现 IDyamicMetaObjectProvider 。在这本书发行之前,我会这样做,但早期的访问版本目前只有 DynamicObject 示例。 Btw,如果你今天买的是一半的价格 - 使用代码 twtr0711 。稍后我会修改此答案以删除该位:)



< / plug>


Given the new dynamic support in C# 4, is it possible to write a class in such a way that if a method is invoked on an instance and that method is not present, dispatch is passed to another method? This might look something like:

public class Apple : ... {
  // ...

  private ... MethodMissing(string name, ...) {
    if (name == "TurnIntoOrange") {
      // do something
    }
  }
}

dynamic d = new Apple();
d.TurnIntoOrange();       // Not actually defined on Apple; will pass to MethodMissing.

Other languages would call this "method_missing support", under the more general heading of metaprogramming. I'm not sure what C# calls this specifically. But is it possible?

解决方案

Absolutely. Either implement IDynamicMetaObjectProvider or derive from DynamicObject for a much simpler route. See the DLR documentation for some good examples.

Here's a quick example of DynamicObject:

using System;
using System.Dynamic;

public class MyDynamic : DynamicObject
{
    public override bool TryInvokeMember
        (InvokeMemberBinder binder,
         object[] args,
         out object result)
    {
        Console.WriteLine("I would have invoked: {0}",
                          binder.Name);
        result = "dummy";
        return true;
    }

    public string NormalMethod()
    {
        Console.WriteLine("In NormalMethod");
        return "normal";
    }
}

class Test
{
    static void Main()
    {
        dynamic d = new MyDynamic();
        Console.WriteLine(d.HelloWorld());
        Console.WriteLine(d.NormalMethod());
    }
}

<plug>

I have a bigger example of DynamicObject in the 2nd edition of C# in Depth but I haven't yet implemented IDyamicMetaObjectProvider. I'll do so before the book's release, but the early access edition only has the DynamicObject example at the moment. Btw, if you buy it today it's half price - use the code twtr0711. I'll edit this answer later on to remove that bit :)

</plug>

这篇关于处理未在动态对象上定义的C#方法(也称为respond_to / method_missing)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆