Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6110251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:29:39+00:00 2026-05-23T14:29:39+00:00

I am trying to intercept a method through a proxy class and am getting

  • 0

I am trying to intercept a method through a proxy class and am getting a TargetException “Object does not match target type”. I believe this is similar to what a framework like PostSharp does, but I want to see if I can do this myself as an excercise at the very least.

The goal in this case is to time the method call using Diagnostics.Stopwatch by wrapping it in a new delegate. Its getting a bit over my head, though.

Here is the delegate which wraps the proxied method:

    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; 
            }; 
    }

And then here is a method which does the interception, essentially creating a new MethodInfo instance that describes the new delegate created here, which is based on whether the Method has a certain attribute indicating it should be timed:

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.

If I do this it works fine (just invoking the MethodInfo without wrapping it):

retVal = method.Invoke( obj, parameters );

Where obj is the instance declaring the method in question, the one decorated with a [Time] attribute.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:29:39+00:00Added an answer on May 23, 2026 at 2:29 pm

    In your Intercept call, you’re creating a new MethodInfo object. That MethodInfo object is completely different from the one that you pass in. It does not come from a class that inherits from the type of the original object (nor does it come from the FunctionInterceptor class). If you instead did something like this:

    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;
    }
    

    It will work because the method in this case actually comes from the proxy’s type. When you call Invoke(interceptor,parameters), the method itself has to be from the type FunctionInterceptor. In this case it is not (you may create it there but FunctionInterceptor is not the declaring type for the new function). In fact the declaring type for the new function is going to be something like ()_<>ClassSomethingOrOther.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to invoke a method on an external webservice (over the Internet) it
I am trying to figure out a clean way to intercept uncaught exceptions that
I am getting this error when trying to run internet information services on a
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
I have looked around on the Internet trying to answer this question. It seems
Trying to create a small monitor application that displays current internet usage as percentage
How to get Hibernate session inside a Hibernate Interceptor? I'm trying to use Hibernate
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to get my css / C# functions to look like this: body {
Trying to find some simple SQL Server PIVOT examples. Most of the examples that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.