包装函数的通用方法

发布于 2024-12-22 02:14:29 字数 226 浏览 1 评论 0 原文

假设我想将一个函数包装在另一个函数中,以便向包装的函数添加一些功能。但我事先不知道返回类型或参数,因为这些方法是作为 Web 服务代理生成的。

我的第一个想法是使用 Func。但某些函数可能会返回 void,在这种情况下 Action 会更合适。

现在我的问题是:是否有一个很好的通用方法来实现这一目标?我需要寻找一些模式吗?

Say I want to wrap a function in another function, so to add some functionality to the wrapped function. But I don't know the return type or parameters on beforehand as the methods are generated as a web service proxy.

My first train of thought was using Func<T>. But some functions might return void, in which case Action<T> would be more appropriate.

Now my question: is there a nice generic way to achieve this? Is there some pattern I need to look for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆蝶 2024-12-29 02:14:29

好吧,我想到了 外观模式...这不是一种非常自动的做事方式,但它有效。基本上,您只需将另一个接口放在代理前面并调用它即可。然后您可以添加任何您想要的功能。

解决此问题的另一种方法是使用面向方面的编程。我过去曾使用 PostSharp (当它免费时)来执行此操作。您可以通过向方法/属性添加属性来执行诸如在函数中添加预处理/后处理之类的操作。然后,AOP 组件使用代码编织来重写您的 IL,以包含您引用的代码。请注意,这会显着减慢构建过程。

Well, the Facade Pattern comes to mind... It's not a very automatic way of doing things, but it works. You basically just put another interface in front of the proxy and call that instead. You can then add any functionality that you desire.

Another way to approach this is with aspect oriented programming. I've used PostSharp (when it was free) to do this this in the past. You can do things like add Pre / Post processing in the function by adding an attribute to a method / property. The AOP components then use code weaving to rewrite your IL to include the code that you've referenced. Note that this can significantly slow the build process.

风轻花落早 2024-12-29 02:14:29

正如您所说“我事先不知道返回类型或参数”,我认为动态代理就是您想要的
需要。

不幸的是,我只知道Java中的动态代理。但我确信 C# 也有类似的东西。

尝试谷歌搜索“动态代理 C#”。

例如,这里似乎有 C# 的实现: http://www.castleproject.org/dynamicproxy/< /a>

那么,什么是动态代理?

来自 JavaDoc http://docs.oracle.com/javase/1.3/docs/guide/reflection/proxy.html#api

动态代理类是一个实现运行时指定的接口列表的类,这样通过该类实例上的接口之一进行的方法调用将被编码并分派到另一个对象通过统一的接口。因此,动态代理类可用于为接口列表创建类型安全的代理对象,而无需预先生成代理类,例如使用编译时工具。动态代理类实例上的方法调用被分派到实例调用处理程序中的单个方法,并且它们使用 java.lang.reflect.Method 对象进行编码,该对象标识所调用的方法和包含参数的 Object 类型数组。

As you say "I don't know the return type or parameters on beforehand", I think a Dynamic Proxy is what you
need.

Unfortunately, I know about the Dynamic Proxy in Java only. But I am sure, there is something similar for C#.

Try Googling "Dynamic Proxy C#".

For example, there seems to be an implementation for C# here: http://www.castleproject.org/dynamicproxy/

So, what IS a Dynamic Proxy?

From the JavaDoc http://docs.oracle.com/javase/1.3/docs/guide/reflection/proxy.html#api:

A dynamic proxy class is a class that implements a list of interfaces specified at runtime such that a method invocation through one of the interfaces on an instance of the class will be encoded and dispatched to another object through a uniform interface. Thus, a dynamic proxy class can be used to create a type-safe proxy object for a list of interfaces without requiring pre-generation of the proxy class, such as with compile-time tools. Method invocations on an instance of a dynamic proxy class are dispatched to a single method in the instance's invocation handler, and they are encoded with a java.lang.reflect.Method object identifying the method that was invoked and an array of type Object containing the arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文