动态实现接口 [英] Dynamicaly implement interfaces

查看:54
本文介绍了动态实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我已经看到有Mock对象的实现,其中
在执行期间即时实现接口。有人可以指点

我在.Net中如何做到这一点的方向?


我会用它来包装对象并存储调用把

对象放到一个文件中。后来我想用这个

文件作为回放数据来模拟对象。


任何提示?


问候,

Achim

解决方案

" Achim Domma(Procoders)" <做*** @ procoders.net>在消息中写道

news:dd ************* @ news.t-online.com ...



我已经看到Mock对象的实现,它们在执行期间即时实现接口。有人能指出我在.Net中如何做到这一点的方向吗?




两种可能性立即浮现在脑海中,几乎可以肯定的是>
其他。


你可以使用Reflection Emit命名空间来动态询问

类,然后动态发出代理/模拟。


其次,您可以从ContextBoundObject派生您的对象,拦截

对您的实例的调用,然后将它们记录到文件中。这是

本质上是面向方面编程的练习。在我的

头的顶部,我猜你的呼叫拦截也可以从文件中读取,

然后伪造一个返回或改变状态。值得注意的是,从ContextBoundObject派生的任何类

都会有糟糕的表现。


-

问候,


Tim Haughton


Agitek
http://agitek.co.uk
http ://blogitek.com/timhaughton


Achim,


不确定为什么要换行一个对象如果你想存储一些

对象状态并回放,那么序列化/反序列化对象。


最好的问候

Johann Blake


Tim Haughton写道:

其次,你可以从ContextBoundObject派生你的对象,拦截对您的实例的调用,然后将它们记录到文件中。这基本上是面向方面编程的练习。在我的头顶上,我猜你的呼叫拦截也可以从文件中读取,然后伪造一个返回或改变状态。值得注意的是,任何派生自ContextBoundObject的类都会有糟糕的表现。




感谢您的回复!性能应该不是问题,因为

的情况,但ContextBoundObject的文档似乎是一个问题。

MSDN中的引用告诉我没有太多关于如何使用它。你知道网上的教程或通过Safari获得的书吗?


问候,

Achim


Hi,

I''ve seen that there are implementations of Mock objects, which
implement interfaces on-the-fly during execution. Could somebody point
me to the direction on how to do that in .Net?

I would to use that to wrap an object and to store the calls to the
object into a file. Later I want to simulate the object by using this
file as "playback data".

Any hints?

regards,
Achim

解决方案

"Achim Domma (Procoders)" <do***@procoders.net> wrote in message
news:dd*************@news.t-online.com...

Hi,

I''ve seen that there are implementations of Mock objects, which
implement interfaces on-the-fly during execution. Could somebody point
me to the direction on how to do that in .Net?



Two possibilities spring immediately to mind, there are almost certainly
others.

You could you the Reflection Emit namespace to dynamically interrogate a
class, then emit a proxy/mock on the fly.

Secondly, you could derive your object from ContextBoundObject, intercept
the calls to your instances, then record them to the file. This is
essentially an exercise in Aspect Oriented Programming. Off the top of my
head, I''m guessing that your call intercepts could also read from the file,
then forge a return or change of state. It''s worth noting that any class
derived from ContextBoundObject will have poor performance.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton


Achim,

Not sure why you want to "wrap an object". If you want to store some
object state and "play it back", then serialize/deserialize the object.

Best Regards
Johann Blake


Tim Haughton wrote:

Secondly, you could derive your object from ContextBoundObject, intercept
the calls to your instances, then record them to the file. This is
essentially an exercise in Aspect Oriented Programming. Off the top of my
head, I''m guessing that your call intercepts could also read from the file,
then forge a return or change of state. It''s worth noting that any class
derived from ContextBoundObject will have poor performance.



Thanks for your response! Performance should be not problem in that
case, but documentation for ContextBoundObject seems to be a problem.
The reference in MSDN tells me not too much about how to use it. Do you
know a tutorial on the web or a book which is available via Safari?

regards,
Achim


这篇关于动态实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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