IOC依赖注入集合属性

person类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t1_IoCTest
{
    //说明:如果需要使用IoC,前提是这个类及使用这个类的地方,不在同一个程序集中

    public interface IPerson
    {
        void Say();
    }

    public partial class Person:IPerson
    {
        //constructor
        public Person()
        {
            
        }

        public Person(string name)
        {
            Name = name;
        }

        public Person(int age)
        {
            Age = age;
        }

        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public string Name { get; set; }
        public int Age { get; set; }
        public Car MyCar { get; set; }

        public List<String> MyCarList { get; set; }    //String类型,若是Car类型,则配置文件中添加对另一注入属性节点的引用

        public void Say()
        {
            //Console.WriteLine("Hello {0},MyCar Brand:{1}",Name,MyCar.Brand);
            foreach(var item in MyCarList){
                Console.WriteLine(item);
            }
        }
    }

    public class Car
    {
        public string Brand { get; set; }
	//重写ToString方法
	public override ToString(){
		return Brand;
	}
    }
}

program。cs:main方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spring.Context;
using Spring.Context.Support;
using Spring.Core;

namespace t1_IoCTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //1、创建容器对象
            IApplicationContext ctx = ContextRegistry.GetContext();

            //2、声明接口类型的变量
            IPerson p;

            //3、通过容器创建对象
            //p = ((IObjectFactory) ctx).GetObject("Zxh") as IPerson;
            p = ctx.GetObject<IPerson>("Zxh");

            //4、完成对象成员的调用
            p.Say();

            Console.ReadKey();
        }
    }
}

app.config配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>
  
  <spring>
    <context>
      <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net">
      <description>An  example that demonstrates simple IoC features.</description>
      <!--type中逗号左边是类的完整名称,右边是类的程序集的名称-->
      <object name="Zxh" type="t1_IoCTest.Person,t1_IoCTest">
        
        
        <property name="MyCarList">
          <list>
            <value>jk</value>
            <value>nll</value>
            <value>yzk</value>
	    <!-- 若前面MyCarList为List<Car>类型,则引用其他注入属性 -->
	    <!-- <ref object="MyCar1"/> -->

          </list>
        </property>
      </object>
      <object name="MyCar1" type="t1_IoCTest.Car,t1_IoCTest">
        <property name="Brand" value="BMW"/>
      </object>
       <object name="MyCar2" type="t1_IoCTest.Car,t1_IoCTest">
        <property name="Brand" value="Audi"/>
      </object>
    </objects>
  </spring>
  
  
</configuration>


集合属性注入结果:


MyCarList改为List<Car>类型后,输出为:


重写Car类的ToString方法后 :



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值