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

  • Home
  • SEARCH
  • 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 994333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:33:30+00:00 2026-05-16T06:33:30+00:00

Let’s say that I have the following interface: public interface IMyService { void SimpleMethod(int

  • 0

Let’s say that I have the following interface:

public interface IMyService
{
   void SimpleMethod(int id);
   int Hello(string temp);

}

And want to generate a class that looks like this (using reflection emit).

public class MyServiceProxy : IMyService
{
  IChannel _channel;

  public MyServiceProxy(IChannel channel)
  {
    _channel = channel;
  }

  public void SimpleMethod(int id)
  {
    _channel.Send(GetType(), "SimpleMethod", new object[]{id});
  }

  public int Hello(string temp)
  {
    return (int)_channel.Request(temp);
  }
}

How do I do it? I’ve checked various dynamic proxies and mock frameworks. They are bit complex and not very easy to follow (and I do not want an external dependency). It shouldn’t be that hard to generate a proxy for an interface. Can anyone show me how?

  • 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-16T06:33:31+00:00Added an answer on May 16, 2026 at 6:33 am

    All in all I’m going to agree with others’ comments. I’ve used Castle’s DynamicProxy and I think it’s wonderful. You can do some really amazing and powerful stuff with it. That said, if you’re still considering writing your own, read on:

    If you’re not excited about emitting IL, there are some new techniques using Lambda expressions that you can use to generate code. None of this is a trivial task, however.

    • C# Dynamic Event Subscription
    • Howto emit a delegate or lambda expression

    Here’s an example of how I’ve used Lambda expressions to generate a dynamic event handler for any .NET event. You could use a similar technique to generate a dynamic interface implementation.

        public delegate void CustomEventHandler(object sender, EventArgs e, string eventName);
    
        Delegate CreateEventHandler(EventInfo evt, CustomEventHandler d)
        {
            var handlerType = evt.EventHandlerType;
            var eventParams = handlerType.GetMethod("Invoke").GetParameters();
    
            //lambda: (object x0, EventArgs x1) => d(x0, x1)
    
            // This defines the incoming parameters of our dynamic method.  
            // The method signature will look something like this:
            // void dynamicMethod(object x0, EventArgs<T> x1)
            // Each parameter is dynamically determined via the 
            // EventInfo that was passed.
            var parameters = eventParams.Select((p, i) => Expression.Parameter(p.ParameterType, "x" + i)).ToArray();
    
            // Get the MethodInfo for the method we'll be invoking *within* our
            // dynamic method.  Since we already know the signature of this method,
            // we supply the types directly.
            MethodInfo targetMethod = d.GetType().GetMethod(
                "Invoke", 
                new Type[] { typeof(object), typeof(EventArgs), typeof(string) }
                );
    
            // Next, we need to convert the incoming parameters to the types
            // that are expected in our target method.  The second parameter,
            // in particular, needs to be downcast to an EventArgs object
            // in order for the call to succeed.
            var p1 = Expression.Convert(parameters[0], typeof(object));
            var p2 = Expression.Convert(parameters[1], typeof(EventArgs));
            var p3 = Expression.Constant(evt.Name);
    
            // Generate an expression that represents our method call.  
            // This generates an expression that looks something like:
            // d.Invoke(x0, x1, "eventName");
            var body = Expression.Call(
                Expression.Constant(d),
                targetMethod,
                p1,
                p2,
                p3
            );
    
            // Convert the entire expression into our shiny new, dynamic method.
            var lambda = Expression.Lambda(body, parameters.ToArray());
    
            // Convert our method into a Delegate, so we can use it for event handlers.
            return Delegate.CreateDelegate(handlerType, lambda.Compile(), "Invoke", false);
        }
    

    Regards,

    -Doug

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

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let's say I have the string: hello world; some random text; foo; How could
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I have 2 functions: void function1(int *ptr) { printf(%d, *ptr); } and
Let's say we have the following: abstract class A; class B : public A;
Let's say we have: @Entity public class Order { @Id private int id; @OneToMany(mappedBy=order)
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>

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.