抓破我的秃头....我有一个通用的界面
interface IGenericInterface<T>
{
T GenericTypeProperty {get; set;}
void PerformService();
}
我有实现通用接口(interface)并做一些事情的服务器类
public class ServeRich : IGenericInterface<RichPeople>
{
RichPeople GenericTypeProperty {get; set;}
void PerformService() { //Serving the rich }
}
public class ServePoor : IGenericInterface<PoorPeople>
{
PoorPeople GenericTypeProperty {get; set;}
void PerformService() { //Serving the Poor }
}
然后我有一个如下所示的服务类,我想调用其中一个服务器类并将其分配给并调用 PerformService()
public class ServeThem
{
//This is where I am trying to figure out how to do this
IGenericInterface<T<-??> Server {get; set;}
}
我想最终创建服务类并在服务器上调用执行服务方法....类似这样
main()
{
ServeThem service= new ServeThem();
service.Server = new ServePoor(); //This will be resolved by Ninject
service.Server.PerformService();
}
我的问题在 IGenericInterface<T<-??> Server {get; set;}
.我想弄清楚如何声明 Server
属性作为通用接口(interface)类型,可以分配给实现特定类型接口(interface)的类的对象。似乎是为了声明 IGenericInterface<type>
,必须提及实际的具体类型。我尝试添加 passedInType
属性(property)和使用typeof(passedInType)
但这会引发异常。
如何将属性声明为通用接口(interface)类型?这可能吗?
最佳答案
您的服务中似乎没有使用GenericTypeProperty
,因此您可以将PerformService
方法移动到非通用接口(interface)并在服务等级
interface INonGenericInterface
{
void PerformService();
}
interface IGenericInterface<T>: INonGenericInterface
{
T GenericTypeProperty {get; set;}
}
public class ServeThem
{
INonGenericInterface Server {get; set;}
}
关于c# - 将 "TYPE"传递给 Generic_Interface 类型的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32894965/