c 中方法注入的一个简单例子

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var foo = new Dependency();
            var bar = new ClassWithDependency();

            bar.DoSomething(foo); //Inject dependency as method parameter

            Console.ReadLine();
        }
    }

    public interface IDependency
    {
        void DoSomething();
    }

    public class Dependency: IDependency
    {
        public void DoSomething()
        {
            Console.WriteLine("Hello");
        }
    }

    public class ClassWithDependency
    {
        public void DoSomething(IDependency dependency)
        {
            dependency.DoSomething();
        }
    }
}