在 C# 中实现动态代理的最佳方法是什么?
我需要在 C# 中创建动态代理。我希望这个类包装另一个类,并采用它的公共接口,转发对这些函数的调用:
class MyRootClass
{
public virtual void Foo()
{
Console.Out.WriteLine("Foo!");
}
}
interface ISecondaryInterface
{
void Bar();
}
class Wrapper<T> : ISecondaryInterface where T: MyRootClass
{
public Wrapper(T otherObj)
{
}
public void Bar()
{
Console.Out.WriteLine("Bar!");
}
}
这是我想要使用它的方式:
Wrapper<MyRootClass> wrappedObj = new Wrapper<MyRootClass>(new MyRootClass());
wrappedObj.Bar();
wrappedObj.Foo();
产生:
Bar!
Foo!
有什么想法吗?
做到这一点最简单的方法是什么?
最好的方法是什么?
非常感谢。
更新
我尝试遵循 Wernight 的建议并使用 C# 4.0 动态代理来实现这一点。不幸的是,我仍然被困住了。代理的目的是模仿(通常,通常)预期的其他接口。使用 DynamicObject 要求我将其所有客户端更改为使用“dynamic”而不是“ISecondaryInterface”。
有没有办法获取代理对象,这样当它包装 A 时,它会(静态地?)宣传它支持 A 的接口;当它包装 B 时,它会通告支持 B 的接口?
更新2
例如:
class MySecretProxy : DynamicObject, ISecondaryInterface
{
public override void TryInvokeMember(...) { .. }
// no declaration of Bar -- let it be handled by TryInvokeMember
}
I've got a need to create a dynamic proxy in C#. I want this class to wrap another class, and take on it's public interface, forwarding calls for those functions:
class MyRootClass
{
public virtual void Foo()
{
Console.Out.WriteLine("Foo!");
}
}
interface ISecondaryInterface
{
void Bar();
}
class Wrapper<T> : ISecondaryInterface where T: MyRootClass
{
public Wrapper(T otherObj)
{
}
public void Bar()
{
Console.Out.WriteLine("Bar!");
}
}
Here's how I want to use it:
Wrapper<MyRootClass> wrappedObj = new Wrapper<MyRootClass>(new MyRootClass());
wrappedObj.Bar();
wrappedObj.Foo();
to produce:
Bar!
Foo!
Any ideas?
What's the easiest way to do this?
What's the best way to do this?
Thanks so much.
UPDATE
I tried following Wernight's recommendation and implement this using C# 4.0 dynamic proxies. Unfortunately, I'm still stuck. The point of the proxy is to mimick the other interface which is (normally, usually) expected. Using DynamicObject requires me to change all the clients of this to use 'dynamic' instead of 'ISecondaryInterface'.
Is there a way to get a proxy object, such that when it wraps an A, it advertises (statically?) that it supports A's interface; and when it wraps a B, it advertises that is supports B's interface?
UPDATE 2
For example:
class MySecretProxy : DynamicObject, ISecondaryInterface
{
public override void TryInvokeMember(...) { .. }
// no declaration of Bar -- let it be handled by TryInvokeMember
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
.NET 4
DynamicObject
可以帮助您实现这一目标。早期的.NET框架可以使用:
请参阅完整文章。
我知道Castle Project的动态代理经常被使用(就像在Moq中只是为了命名一个大型项目)。
回复更新的主题
您编写的内容将无法编译。 动态代理是运行时生成的代码,因此您必须以某种方式创建要代理的类的具体实例。也许您正在寻找AOP(面向方面的编程)。
.NET 4
DynamicObject
can help you achieving that.Earlier .NET framework can use:
See the full article.
I know that Castle Project's Dynamic Proxy is often used (like in Moq just to name one large project).
REPLY TO UPDATED TOPIC
What you wrote will not compile. Dynamic proxies are runtime generated code, so you'll have to create a concrete instance of the class you're proxying some way or another. May be you're looking to do AOP (aspect-oriented programming).
你看过Castle项目的DynamicProxy吗?它可能会提供您最终想要实现的目标。参见 http://www.castleproject.org/dynamicproxy/index.html
它也是开放的源,以便您甚至可以在需要时分叉它。
Have you looked at the Castle project's DynamicProxy? It may provide what you're ultimately trying to achieve. See http://www.castleproject.org/dynamicproxy/index.html
It's also open source so you could even fork it if required.
如果目标类型是接口或派生自 MarshalByRefObject,则可以使用 RealProxy 执行此操作。
You can do this with RealProxy if the target Type is an interface or derives from MarshalByRefObject.
您可能需要查看 linfu,它包含动态代理机制。
You may want to look at linfu which contains a dynamic proxy mechanism.
的代理
我知道nhibernate用于延迟加载Castle
Linfu
Spring ByteCode
I know the proxies that used by nhibernate for lazy loading
Castle
Linfu
Spring ByteCode