English 中文(简体)
2. 采用具有反省的方法
原标题:Intercepting a method with reflection

我试图通过代理班拦截一种方法,正在获得“目标与目标类型不符”的“目标”。 我认为,这与《沙普协议》这样的框架相似,但我想看到,我能否至少做事。

本案的目标是利用诊断方法使用这种方法。 停止监视,在新任代表中总结。 然而,它却超越了我的头部。

代表在此总结近似方法:

    public static Func<Object> Time(this MethodInfo target, object parent, object[] parameters, Action<string> logger)
    {
        return delegate
            {
                Stopwatch s = new Stopwatch();

                s.Start();
                object value = target.Invoke(parent, parameters);
                s.Stop();

                logger("Elapsed ms for function  " + target.Name + "  = " + s.ElapsedMilliseconds.ToString());

                return value; 
            }; 
    }

那么,这里是一种拦截方法,主要是建立一个新的方法信息案例,说明在此设立的新代表,其依据是,方法是否具有某种特性,表明应当有时间:

public class FunctionInterceptor
{
    public MethodInfo Intercept(Object proxy, MethodInfo method, Object[] args)
    {
        return new Func<Object>(() =>
        {
            var data = method.GetCustomAttributes(typeof(TimeAttribute), true);

            object result = default(object);

            foreach (object d in data)
            {
                if (d.GetType() == typeof(TimeAttribute)) // [Time] attribute
                {
                    result = method.Time(proxy, args, Log.Write).DynamicInvoke(args);
                }
            }
            return result;

        }).Method;  // returning MethodInfo of this delegate
    }

Now it seems to me I should be able to invoke the delegate that this MethodInfo object describes:

 var interceptor = new FunctionInterceptor();

 retVal = interceptor.Intercept(proxy, method, parameters).Invoke(interceptor, parameters); 

But I am getting the error - Object does not match target type. I examined the MethodInfo instance and the DeclaringType is FunctionInterceptor, meaning I should be passing in the instance of the interceptor as above. Not sure what the problem is.

如果我这样做的话,它会做一些细微的工作(在不作总结的情况下援引“方法”):

retVal = method.Invoke( obj, parameters );

如果是宣布相关方法的,则与[时间]属性有关。

感谢。

最佳回答

在您的《感受》呼吁中,你重新树立了一种新的方法。 这种方法的目的与你通过的方法完全不同。 它不来自原物体类型继承的类别(也不来自功能使用者类别)。 如果是这样的话:

public object Intercept(object proxy, MethodInfo method, object[] args)
{
    var data = method.GetCustomAttributes(typeof(TimeAttribute), true);

    object result = default(object);

    foreach (object d in data)
    {
        if (d.GetType() == typeof(TimeAttribute)) // [Time] attribute
        {
            result = method.Time(proxy, args, Log.Write).DynamicInvoke(args);
        }
    }
    return result;
}

它将发挥作用,因为本案中的方法实际上来自代理类型。 当你打电话给Invoke(截肢)时,这种方法本身必须来自功能使用者的类型。 在这种情况下,它不是(你可以在那里设立,但功能使用者不是新职能的申报类型)。 事实上,新职能的申报类型将像()_<>ClassSomethingOr Other。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签