使用Reflection.Emit实现接口

3

假设我有以下接口:

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

}

我希望生成一个类,其样式如下(使用反射发出)。

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

我该怎么做?我已经查看了各种动态代理和模拟框架。它们有点复杂,不太容易理解(而且我不想要外部依赖)。生成接口的代理应该不难。有人能向我展示如何做吗?


一个动态代理框架应该是实现这个的最简单的方法。否则,我建议编译您的示例实现,并通过.NET反射器或ILDASM检查生成的IL。那样可以给您一个需要发出什么的想法。 - Daniel Pratt
我完全同意丹尼尔的观点。你使用过Castle的DynamicProxy吗?只需几行代码即可开始使用。这真的太复杂了吗?(为什么要讨厌外部依赖?有时候,已经编写并免费提供了最适合工作的工具。为什么不使用它呢?) - Kirk Woll
1
在这种情况下,Castle DynamicProxy 是我选择的工具。维护 Reflection.Emit 代码并不好玩。尽管如此,我尊重您对依赖关系的担忧。Mike Barnett 给了我们 ILMerge(例如 RhinoMocks 使用 DynamicProxy,但将其合并到其程序集中以提高可重用性)。 - Just another metaprogrammer
1个回答

3
总的来说,我同意其他人的评论。我使用过Castle的DynamicProxy,我认为它非常棒。你可以用它做一些非常惊人和强大的事情。话虽如此,如果你仍在考虑编写自己的代码,请继续阅读:
如果你对发射IL不感兴趣,那么有一些使用Lambda表达式的新技术可以用来生成代码。然而,这并不是一个简单的任务。
以下是我如何使用Lambda表达式为任何.NET事件生成动态事件处理程序的示例。你可以使用类似的技术来生成动态接口实现。
    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);
    }

问候,

-道格


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接