处理未在动态对象上定义的C#方法(又称为respond_to / method_missing) [英] Handling a C# method that isn't defined on a dynamic object (aka respond_to/method_missing)
问题描述
给定C#4中的新动态支持,是否有可能以这样的方式编写一个类,如果一个方法被调用一个实例,并且该方法不存在,dispatch被传递到另一个方法?这可能类似:
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文档。
下面是 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;
return 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());
}
}
plug>
我有一个更大的例子: 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屋!