有没有一种方法可以创建支持接口的DynamicObject? [英] Is there a way to create a DynamicObject that supports an Interface?
问题描述
是否可以定义一个从DynamicObject派生并支持接口(ICanDoManyThings)的类,而无需在接口中实现每个方法?
Can I define a class which derives from DynamicObject and supports an interface (ICanDoManyThings) without having to implement each method in the interface?
我正在尝试创建一个动态代理对象,并希望通过MyProxyClass.TryInvokeMember的实现来处理此类的方法调用,该实现可以将也可以不将其传递给包装的对象.
I'm trying to make a dynamic proxy object, and want the method invocations on this class to be handled by the implementation of MyProxyClass.TryInvokeMember, which may or may not pass them on to a wrapped object.
这可能吗?
谢谢
推荐答案
ImpromptuInterface 确实做到了这一点可以与任何IDynamicMetaObjectProvider一起使用,包括DynamicObject子类和ExpandoObject.
ImpromptuInterface does exactly this and it works with ANY IDynamicMetaObjectProvider including DynamicObject subclasses and ExpandoObject.
using ImpromptuInterface;
using ImpromptuInterface.Dynamic;
public interface IMyInterface{
string Prop1 { get; }
long Prop2 { get; }
Guid Prop3 { get; }
bool Meth1(int x);
}
...
//Dynamic Expando object
dynamic tNew = Build<ExpandoObject>.NewObject(
Prop1: "Test",
Prop2: 42L,
Prop3: Guid.NewGuid(),
Meth1: Return<bool>.Arguments<int>(it => it > 5)
);
IMyInterface tActsLike = Impromptu.ActLike<IMyInterface>(tNew);
Linfu实际上不会使用基于DLR的对象,而是使用它自己的幼稚的后期绑定,这给它带来了 SERIOUS 的性能成本. Clay确实使用了dlr,但是您必须坚持使用Clay对象,这些对象是为将所有行为注入到ClayObject中而设计的,这并不总是那么简单.
Linfu won't actually use DLR based objects and rather uses it's own naive late binding which gives it a SERIOUS performance cost. Clay does use the dlr but you have to stick with Clay objects which are designed for you to inject all behavior into a ClayObject which isn't always straightforward.
这篇关于有没有一种方法可以创建支持接口的DynamicObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!