-
StackOverflow 文档
-
dependency-injection 教程
-
方法注入
-
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();
}
}
}