我尝试使用C#DI方法来实现一些东西.以下是我的代码片段.
public interface IMessageService
{
void Send(string uid, string password);
}
public class MessageService : IMessageService
{
public void Send(string uid, string password)
{
}
}
public class EmailService : IMessageService
{
public void Send(string uid, string password)
{
}
}
Run Code Online (Sandbox Code Playgroud)
和代码创建一个ServiceLocator:
public static class ServiceLocator
{
public static object GetService(Type requestedType)
{
if (requestedType is IMessageService)
{
return new EmailService();
}
else
{
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我用它创建一个测试代码
public class AuthenticationService
{
private IMessageService msgService;
public …Run Code Online (Sandbox Code Playgroud) c# ×1