2016-09-16 53 views
1

我在使用泛型的地方实现了多个接口。以下是我的代码泛型中的多个接口

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) 
    { } 
} 

现在,我无法在Main方法中创建主类的实例。我试图但不能做到。

class Program 
{ 
    static void Main(string[] args) 
    { 
     createGenericClass<IWebService> obj = new createGenericClass<IWebService>(); 
    } 
} 

请建议。

+0

”不行“,你能告诉我们会发生什么吗?如果编译器抱怨,你需要告诉我们实际的编译器错误。 –

回答

3

解决方案1 ​​

您可以通过使用通用接口来解决此问题。该接口将由您的帮助程序(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); 
} 

所以,你可以实例化助手类是这样的:

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 
    } 
} 

注:也有几个步骤,你可以用它来简化你的使用辅助类,如做没有泛型的辅助类static(也许是Singleton),仅在方法上使用泛型。这样你就不需要实例化类。
然而,这些“简化”修改性能和可用性将取决于你的目标,在使用的情况下,对数据和其他管理类您正在使用等工作..


解决方案2 - (你的任务可以在没有泛型的情况下解决)

你说你不想转换收到的实例。既然你想在这些实例上调用不同的方法,你可以通过在你的helper类上调用不同的方法来调用它们。在这种情况下,你并不需要泛型。

这将完成这项工作,而不使用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); 
    } 
} 

解决方案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); 
    } 
} 

用法:

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); 
} 
+0

谢谢你的答复。在你的情况下,你正在进行铸造,这对性能不利。请告诉我如何实例化这个类“公共类createGenericClass 其中T1:IWebService,IVoiceService” – KiddoDeveloper

+0

如果我有一个条件,我需要实现两个或多个接口,我将如何创建一个实例? – KiddoDeveloper

+0

我已经更新了我的答案,可以在不投射的情况下工作。如果它不适合你的目的,并且你不想投入类型,那么你可能不得不使用接口 - 就像我对IBaseService所做的那样。您必须对方法进行分组,因此您可以在ONE界面上对其进行调用。 – KAI

4

随着你的设计类型T需要是IWebServiceIVoiceService。您尝试初始化obj的方式,您只使用IWebService,而不是IVoiceService

一个解决这个问题的方法是将通用类分割成多个泛型类 - 一个用于IWebService,另一个用于IVoiceService

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

public class createGenericClassVoice<T> where T : IVoiceService 
{ 
    public void CallDoSomeThingElse(T t, int a, float b) 
    { 
     t.DoSomeThingElse(a, b); 
    } 
} 

然后你可以使用它们像这样:

createGenericClassWeb<IWebService> objWeb = new createGenericClassWeb<IWebService>(); 
createGenericClassVoice<IVoiceService> objVoice = new createGenericClassVoice<IVoiceService>(); 

虽然在这种情况下,你不清楚为什么你需要泛型,你可以使用这样简单的类:

public class createNonGenericClassWeb 
{ 
    public void CallDoSomeThingElse(IWebService t, int a, float b) 
    { 
     t.DoSomeThing(a, b); 
    } 
} 
+1

好的。但有没有什么办法可以调用单个实例/对象,我已经实现了两个接口“公共类createGenericClass 其中T1:IWebService,IVoiceService” – KiddoDeveloper

0

你cann OT使用语法创建一个新的实例,因为你喜欢的类型“IWebService”不匹配

createGenericClass<IWebService> obj = new createGenericClass<IWebService>(); 

createGenericClass采取所谓的“T”类型参数的约束“IWebService,IVoiceService”。 T上的约束要求它实现IWebService和IVoiceService。这意味着您为T参数提供的类型必须是实现这两个接口的类型。

例如:

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

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

类以上的同时实现了IWebService和IVoiceService,所以它传递的约束,你将能够创建一个实例。

createGenericClass<Voice_And_Web_Service> obj = new createGenericClass<Voice_And_Web_Service>(); 

我相信其他的答案是优秀的,涵盖了很多你所面临的问题,但我没有看到一个涵盖正是你问是“不能创建的实例的问题主类“