Castle.DynamicProxy的二次开发

本文不是介绍如何使用Castle.DynamicProxy库,而是介绍利用它进行二次开发,下面通过一个例子,讲解如何实现。
例子的场景:实例化一个类,该类实现了某一个接口,通过接口实现业务功能的调用。下面分别以传统和DynamicProxy两种方式介绍。
(一)传统的方式
接口和类的定义:
 public interface IHello
    {
        string GetString(string msg);
    }

 public class Hello:IHello
 {
  public string GetString(string msg)
  {
   string str = "Hello ";
   return str + msg;
  }
 }
场景实现:
 IHello hello = new Hello() as IHello;
 MessageBox.Show(hello.GetString());
(二)DynamicProxy方式
利用DynamicProxy二次开发,实现上面的场景,首先只定义接口IHello,而不定义实现类Hello,Hello类的定义利用DynamicPorxy
动态实现,
 [CLSCompliant(false)]
    public class MyGenerator
    {
        public static IHello GenerateHello()
        {
            ModuleScope scope = new ModuleScope();
            EasyType typeBuilder = new EasyType(scope, "MyHello", typeof(object), new Type[] { typeof(IHello)});

            GenerateHelloMethod(typeBuilder);
            Type type = typeBuilder.BuildType();

            return Activator.CreateInstance(type) as IHello;
        }

        protected static void GenerateHelloMethod(EasyType tBuilder)
        {
            Type strType = typeof(string);
            MethodInfo strConcat = typeof(string).GetMethod("Concat", new Type[] { strType, strType });

            EasyMethod method = tBuilder.CreateMethod("GetString",
                        MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.NewSlot,
                        new ReturnReferenceExpression(strType), strType);
            LocalReference temp_local = method.CodeBuilder.DeclareLocal(strType);

            AssignStatement stmtAssign = new AssignStatement(temp_local, new FixedReference("Hello ").ToExpression());
            method.CodeBuilder.AddStatement(stmtAssign);

            MethodInvocationExpression invokExpress = new MethodInvocationExpression(temp_local, strConcat, method.Arguments[0].ToExpression());
            stmtAssign = new AssignStatement(temp_local, invokExpress);
            method.CodeBuilder.AddStatement(stmtAssign);

            method.CodeBuilder.AddStatement(new ReturnStatement(temp_local));
        }
    }
场景的实现:
 IHello hello = MyGenerator.GenerateHello();
 MessageBox.Show(hello.GetString());

当然上面的例子没有什么实用价值,只是为了说明如何利用DynamicProxy库进行二次开发,起一个抛砖引玉的作用。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值