C# 在C中实现泛型#

C# 在C中实现泛型#,c#,generics,collections,C#,Generics,Collections,我是一名学生,希望在我的测试项目中实现泛型。下面是我的代码 class clsCallService { public void CallServiceMethod(string serviceName) { if (serviceName == "WEB") Web_Service.AddData("4G", DateTime.Now, DateTime.Now, "test"); else if (serviceName

我是一名学生,希望在我的测试项目中实现泛型。下面是我的代码

class clsCallService
{
    public void CallServiceMethod(string serviceName)
    {
        if (serviceName == "WEB")
            Web_Service.AddData("4G", DateTime.Now, DateTime.Now, "test");
        else if (serviceName == "VOICE")
            Voice_Service.AddData("4G", DateTime.Now, DateTime.Now, "test");
        else if (serviceName == "VIDEO")
            Video_Service.AddData("4G", DateTime.Now, DateTime.Now, "test");
    }
}       

class Web_Service
{
    public static void AddData(string technology, DateTime SDate, DateTime EDate, String someVariable)
    { 
        //Call DAL method
    }
}

class Voice_Service
{
    public static void AddData(string technology, DateTime SDate, DateTime EDate, String someVariable)
    {
        //Call DAL method
    }
}

class Video_Service
{
    public static void AddData(string technology, DateTime SDate, DateTime EDate, String someVariable)
    {
        //Call DAL method
    }
}
现在,我想创建一个泛型类。在这个类中,应该有一个具有泛型参数的泛型方法。所以,如果条件像上面这样,我就不需要写了。我只需要传递任何类和类型的基的对象,就会调用方法。我正试图写一些代码,但无法做到

public class testGenericClass<T> where T : Web_Service, new()
{
    public void CallServiceMethod(string technology, DateTime SDate, DateTime EDate, String someVariable)
    {
        T.AddData(technology, SDate, EDate, someVariable);
    }
}
公共类testGenericClass其中T:Web\u服务,new()
{
public void CallServiceMethod(字符串技术、日期时间SDate、日期时间EDate、字符串变量)
{
T.AddData(技术、SDate、EDate、某些变量);
}
}
请建议


收到一些有用的回复后更新--

更新代码

public class createGenericClass<T> where T : IWebService,IVoiceService
{
    public void CallDoSomeThing(T t, int x, int y)
    {
        t.DoSomeThing(x, y);
    }

    public void CallDoSomeThingElse(T t, int a, float b)
    {
        t.DoSomeThingElse(a, b);
    }

}

public interface IWebService
{
    void DoSomeThing(int x, int y);        
}

public interface IVoiceService
{
    void DoSomeThingElse(int a, float b);
}

public class Web_Service : IWebService
{
    public void DoSomeThing(int x, int y)
    { }    
}    

public class Voice_Service : IVoiceService
{
    public void DoSomeThingElse(int a, float b)
    { }
}
public类createGenericClass其中T:IWebService,IVoiceService
{
公共无效调用值(T T,int x,int y)
{
t、 剂量测定法(x,y);
}
公共无效调用集合(T T、int a、浮动b)
{
t、 DoSomeThingElse(a,b);
}
}
公共接口IWebService
{
无效剂量测定(int x,int y);
}
公共接口IVoiceService
{
无效剂量(内部a,浮动b);
}
公共类Web_服务:IWebService
{
公共无效剂量测定(整数x,整数y)
{ }    
}    
公共类语音服务:IVoiceService
{
公共无效DoSomeThingElse(内部a,浮动b)
{ }
}
现在,我有两个不同的界面,即网络和语音。两者都有不同的功能。我已经成功地实现了它,但是我无法调用我的方法。我不知道怎么打电话。下面的代码(只是尝试创建一个新实例。)

类程序
{
静态void Main(字符串[]参数)
{
createGenericClass obj=新建createGenericClass();
}
}

请建议

您不能对泛型类型调用静态函数,因此我假设您正在尝试调用实例方法。在调用
.AddData(…)
之前,需要实例化
T

公共类testGenericClass其中T:Web\u服务,new()
{
public void CallServiceMethod(字符串技术、日期时间SDate、日期时间EDate、字符串变量)
{
新的T().AddData(技术、SDate、EDate、某些变量);
}
}
类网络服务
{
public void AddData(字符串技术、日期时间SDate、日期时间EDate、字符串变量)
{ 
//调用DAL方法
}
}

您无法对泛型类型调用静态函数,因此我假设您正在尝试调用实例方法。在调用
.AddData(…)
之前,需要实例化
T

公共类testGenericClass其中T:Web\u服务,new()
{
public void CallServiceMethod(字符串技术、日期时间SDate、日期时间EDate、字符串变量)
{
新的T().AddData(技术、SDate、EDate、某些变量);
}
}
类网络服务
{
public void AddData(字符串技术、日期时间SDate、日期时间EDate、字符串变量)
{ 
//调用DAL方法
}
}

声明一个所有类都将实现的接口,如

interface IService 
{
    void AddData(string technology, DateTime SDate, DateTime EDate, String someVariable); 
}
然后每个类都实现,如:

public class Web_Service : IService
{
    public void AddData(string technology, DateTime SDate, DateTime EDate, String someVariable) {
        // do stuff... 
    }
}
// etc...
我认为值得指出的是,如果您使用这样的接口,您可以只接受
IService
类型的参数,而不需要任何泛型

public void CallServiceMethod(IService service, string technology, DateTime SDate, DateTime EDate, String someVariable) 
{
    service.AddData(technology, SDate, EDate, someVariable); 
}

如果出于某种原因需要使用泛型方法,那么可以编写泛型方法,并受接口的约束

public class testGenericClass<T> where T : IService
{
    public void CallServiceMethod(T t, string technology, DateTime SDate, DateTime EDate, String someVariable)
    {
        t.AddData(technology, SDate, EDate, someVariable);
    }
}
公共类testGenericClass,其中T:IService
{
public void CallServiceMethod(T,字符串技术,DateTime SDate,DateTime EDate,string someVariable)
{
t、 AddData(技术、SDate、EDate、某些变量);
}
}

请注意,此方法要求接口方法
AddData()
是一个实例方法,而不是像现在这样的静态方法。

声明一个所有类都将实现的接口,如

interface IService 
{
    void AddData(string technology, DateTime SDate, DateTime EDate, String someVariable); 
}
然后每个类都实现,如:

public class Web_Service : IService
{
    public void AddData(string technology, DateTime SDate, DateTime EDate, String someVariable) {
        // do stuff... 
    }
}
// etc...
我认为值得指出的是,如果您使用这样的接口,您可以只接受
IService
类型的参数,而不需要任何泛型

public void CallServiceMethod(IService service, string technology, DateTime SDate, DateTime EDate, String someVariable) 
{
    service.AddData(technology, SDate, EDate, someVariable); 
}

如果出于某种原因需要使用泛型方法,那么可以编写泛型方法,并受接口的约束

public class testGenericClass<T> where T : IService
{
    public void CallServiceMethod(T t, string technology, DateTime SDate, DateTime EDate, String someVariable)
    {
        t.AddData(technology, SDate, EDate, someVariable);
    }
}
公共类testGenericClass,其中T:IService
{
public void CallServiceMethod(T,字符串技术,DateTime SDate,DateTime EDate,string someVariable)
{
t、 AddData(技术、SDate、EDate、某些变量);
}
}

请注意,此方法要求接口方法
AddData()
是一个实例方法,而不是像现在这样的静态方法。

当您查看.NET基类库中泛型的使用方式时,您会发现它主要用于枚举和集合(例如
列表
IEnumerable
),即,无论底层类型如何,行为相同的数据结构。经验法则通常是:如果您需要在泛型方法中检查确切的
typeof(T)
,那么您可能做错了

然而,在泛型方法中检查
typeof(T)
的一个原因是在工厂方法中,也就是说,它允许您获得特定类型的具体实例。这意味着您将拥有一个特殊的类,该类在程序启动时注册各种接口的具体实现,以便您的程序可以获得具体的依赖项,而不知道它们是如何实例化的

虽然纯控制反转实际上是通过实现的,但服务定位器方法提供了更好的设计,只需手动恢复依赖项(使用
new
关键字)。但是,单元测试稍微困难一些,因为您的

考虑以下情况:

public interface IServiceLocator
{
    T GetService<T>();
}

// you use it whenever you would use `new` otherwise,
// to get a concrete instance of some interface, and
// you don't know and don't care where this instance 
// came from:

var webService = ServiceLocator.GetService<IWebService>();
var videoService = ServiceLocator.GetService<IVideoService>();
可实施为:

public interface IDataService 
{
    void AddData(...);
}

public interface IServiceLocator
{
    IDataService GetServiceByName(string name);
}

public class ServiceLocator : IServiceLocator
{
    readonly Dictionary<string, Func<IDataService>> _typeFactories = 
        new Dictionary<string, Func<IDataService>>();

    public ServiceLocator()
    {
        _typeFactories["WEB"] = () => new SomeWebService();
        _typeFactories["VIDEO"] = () => new SomeVideoService();
        _typeFactories["VOICE"] = () => SomeVoiceService.Instance; // singleton            
    }

    public IDataService GetServiceByName(string name)
    {
        var factory = _typeFactories[name];
        return factory();
    }
}
我建议采用第三种方法。了解,您通常甚至不需要一个花哨的DI框架。

当您了解gen如何
// bad
public SomeClassConstructor()
{
    _service = new WebService();
}

// better
public SomeClassConstructor(IServiceLocator serviceLocator)
{
    _service = serviceLocator.GetService<IWebService>();
}

// best
public SomeClassConstructor(IWebService webService)
{
    _service = webService;
}