Kid*_*per 2 c# generics interface
我使用泛型实现了多个接口。下面是我的代码
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)
{ }
}
Run Code Online (Sandbox Code Playgroud)
现在,我无法在 Main 方法中创建主类的实例。我正在尝试,但我做不到。
class Program
{
static void Main(string[] args)
{
createGenericClass<IWebService> obj = new createGenericClass<IWebService>();
}
}
Run Code Online (Sandbox Code Playgroud)
请建议。
解决方案1
您可以通过使用通用接口来解决这个问题。该接口将由您的 helper ( ) 类实例使用createGenericClass,并且也将由您想要传递的其他接口使用。我们就这样称呼它吧IBaseService。
public class createGenericClass<T>
where T : IBaseService // telling T can be only IBaseService and inherited stuff.
{
public void CallDoSomeThing(T t, int x, int y)
{
(t as IWebService)?.DoSomeThing(x, y); // casting and validating
}
public void CallDoSomeThingElse(T t, int a, float b)
{
(t as IVoiceService)?.DoSomeThingElse(a, b); // casting and validating
}
}
public interface IBaseService{} // only gives a common parent to other interfaces. Common properties, methods can be included here.
public interface IWebService : IBaseService // inheriting the common interface
{
void DoSomeThing(int x, int y);
}
public interface IVoiceService : IBaseService // inheriting the common interface
{
void DoSomeThingElse(int a, float b);
}
Run Code Online (Sandbox Code Playgroud)
因此,您可以这样实例化您的帮助器类:
class Program
{
static void Main(string[] args)
{
var obj = new createGenericClass<IBaseService>();
obj.CallDoSomeThing(new Web_Service(), 1, 2); // works well
obj.CallDoSomeThingElse(new Voice_Service(), 3, 4); // works well
}
}
Run Code Online (Sandbox Code Playgroud)
注意:此外,您还可以使用几个步骤来简化帮助程序类的使用,例如使帮助程序类静态(可能是单例)而不使用泛型,并且仅在方法上使用泛型。这样您就不需要实例化该类。
然而,这些“简化”修改的性能和可用性将取决于您的目标、使用上下文、数据和您正在使用的其他管理器类等。
解决方案 2 - (无需泛型即可解决您的任务)
您说过您不想投射收到的实例。由于您想在这些实例上调用不同的方法,因此您可以通过在辅助类上调用不同的方法来调用这些方法。在这种情况下,您实际上并不需要泛型。
IBaseService这不需要使用甚至泛型就可以完成工作。
public class createGenericClass
{
public void CallDoSomeThing(IWebService t, int x, int y)
{
t.DoSomeThing(x, y);
}
public void CallDoSomeThingElse(IVoiceService t, int a, float b)
{
t.DoSomeThingElse(a, b);
}
}
Run Code Online (Sandbox Code Playgroud)
解决方案3 - (双通用的东西,这里不推荐)
既然你已经要求了:
public class createGenericClass<T, V>
where T: IWebService
where V: IVoiceService
{
public void CallDoSomeThing(T t, int x, int y)
{
t.DoSomeThing(x, y);
}
public void CallDoSomeThingElse(V t, int a, float b)
{
t.DoSomeThingElse(a, b);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
static void Main(string[] args)
{
var obj = new createGenericClass<IWebService, IVoiceService>();
obj.CallDoSomeThing(new Web_Service(), 1, 2);
obj.CallDoSomeThingElse(new Voice_Service(), 3, 4);
}
Run Code Online (Sandbox Code Playgroud)