使用反射来获取方法名称和参数射来、名称、参数、方法

2023-09-02 21:31:04 作者:谁说男人没女人妖娆?

我想锻炼的方式来编程方式创建一个关键 Memcached的的基础上,方法名称和参数。所以,如果我有一个方法,

I am trying to workout a way to programatically create a key for Memcached, based on the method name and parameters. So if I have a method,

string GetName(int param1, int param2);

这将返回:

string key = "GetName(1,2)";

我知道你可以使用反射获得MethodBase,但我如何才能在字符串中的参数值,而不是参数类型?

I know you can get the MethodBase using reflection, but how do I get the parameters values in the string, not the parameter types?

推荐答案

您正在寻找的是一个拦截器。就像它的名字说,拦截器拦截方法调用,并允许您之前的方法被调用后执行的东西。这是许多缓存和日志框架很受欢迎。

What you're looking for is an interceptor. Like the name says, an interceptor intercepts a method invocation and allows you to perform things before and after a method is called. This is quite popular in many caching and logging frameworks.