InjectionProperty是个什么玩意?---一次关于InjectionProperty的实验

 

  说实话,在写这篇文章的时候,对于什么是InjectionProperty我也没有一个好的文字解释。但是我想看了下面我的实验,相信您一定能领悟InjectionProperty的含义。

  先看代码

         container = new UnityContainer();
            container.RegisterType<ALog, Log>();
            container.RegisterType<ILogDao, LogDao>(new InjectionProperty("Al",new ResolvedParameter<Log>()));
            container.RegisterType<IDataBase, DataBase>();
            ALog al = container.Resolve<ALog>();
            Console.WriteLine("Singe Alog:" + al.GetHashCode().ToString());
            LogDao daoLog = (LogDao)container.Resolve<ILogDao>();
            Console.WriteLine("Alog daoLog:"+daoLog.Al.GetHashCode().ToString());
            daoLog.Al = al;
            Console.WriteLine("R daoLog:" + daoLog.Al.GetHashCode().ToString());

  

    首先ALog是个抽象类,实现类为Log;ILogDao是接口,实现类是LogDao;IDataBase也是接口,实现类是DataBase

    在LogDao中,有ALog类的属性Al,并且,在LogDao中有唯一的,以ALog类型为参数的构造函数。

    代码运行后,结果如下:

    Singe Alog:45653674

    Alog daoLog:35287174

    R daoLog:45653674

    从结果看,通过InjectionProperty向LogDao的实力中,注入了一个类型为Log,其属性均为空的对象。

    难道这就是InjectionProperty的作用?

转载于:https://www.cnblogs.com/yriyr/archive/2010/03/24/1694212.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值