Ninject简单的Demo

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

namespace NinjectDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            IKernel kernel = new StandardKernel();
            kernel.Bind<IHuman>().To<Chinese>()              //依赖性链
                .WithPropertyValue("Capital", "北京")          //指定属性的参数值
                .WithPropertyValue("SkinColor","黄色")
                .WithConstructorArgument("count", "13");         //指定构造函数的参数值

            //kernel.Bind<IHuman>().To<USA>().WithPropertyValue("Capital", "华盛顿");
            //kernel.Bind<IHuman>().To<Japanese>().WithPropertyValue("Capital", "东京");



            IHuman human = kernel.Get<IHuman>();
            human.Talk();

            Console.ReadKey();
        }
    }

    //实体类
    public class Person
    {
        public string Name { get; set; }
        public string Language { get; set;}
    }

    //接口

    public interface IHuman
    {
        void Talk();
    }

    //实现接口的类

    public class Chinese : IHuman
    {
        public string Capital { get; set; }  //首都
        public string SkinColor { get; set; } //肤色

        private string Population { get; set; } //人口

        public Chinese(string count)  //构造函数
        {
            Population = count;
        }

        public void Talk()
        {
            Console.WriteLine("中国人说,你好!");
            Console.WriteLine("首都:{0}",this.Capital);
            Console.WriteLine("肤色:{0}",this.SkinColor);
            Console.WriteLine("中国共有{0}亿人",this.Population);
        }
    }

    public class USA : IHuman
    {
        public string Capital { get; set; }
        public void Talk()
        {
            Console.WriteLine("美国人说,Hello!");
            Console.WriteLine("首都:{0}", this.Capital);
        }
    }

    public class Japanese : IHuman
    {
        public string Capital { get; set; }
        public void Talk()
        {
            Console.WriteLine("日本人说,こんにちは!");
            Console.WriteLine("首都:{0}", this.Capital);
        }
    }



}
原文地址:https://www.cnblogs.com/james641/p/5258771.html