小编Ter*_*Liu的帖子

关于输入C#

我尝试使用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#

9
推荐指数
2
解决办法
489
查看次数

标签 统计

c# ×1