将 "TYPE" 传递给 Generic_Interface 类型 属性
Passing in a "TYPE" to a Generic_Interface type Property
抓紧我的秃头....我有一个通用界面
interface IGenericInterface<T>
{
T GenericTypeProperty {get; set;}
void PerformService();
}
我有服务器 classes 实现通用接口并做一些事情
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 }
}
然后我有一个服务 class 如下,我想调用并分配其中一个服务器 class 并调用 PerformService()
public class ServeThem
{
//This is where I am trying to figure out how to do this
IGenericInterface<T<-??> Server {get; set;}
}
我想最终创建服务 class 并在服务器上调用执行服务方法....类似这样
main()
{
ServeThem service= new ServeThem();
service.Server = new ServePoor(); //This will be resolved by Ninject
service.Server.PerformService();
}
我的问题在IGenericInterface<T<-??> Server {get; set;}
。我试图弄清楚如何将 Server
属性 声明为通用接口类型,可以将其分配给实现特定类型接口的 class 对象。似乎为了声明 IGenericInterface<type>
,必须提及一个实际的具体类型。我尝试添加 passedInType
属性 并使用 typeof(passedInType)
但这会引发异常。
如何将 属性 声明为通用接口类型?这可能吗
看来你的服务中没有使用GenericTypeProperty
,所以你可以将PerformService
方法移动到非泛型接口,并在服务中使用此接口class
interface INonGenericInterface
{
void PerformService();
}
interface IGenericInterface<T>: INonGenericInterface
{
T GenericTypeProperty {get; set;}
}
public class ServeThem
{
INonGenericInterface Server {get; set;}
}
您正在寻找 covariance。
// T is covariant (i.e. the "out" keyword)
public interface IGenericInterface<out T>
{
// Check that I removed the setter. T wouldn't be covariantly valid
// if it could be set...
// BTW, this doesn't prevent an implementation to provide a setter.
T GenericTypeProperty { get; }
void PerformService();
}
现在将您的 ServeThem
class 声明为整个 属性 为 IGenericInterface<object>
:
public class ServeThem
{
//This is where I am trying to figure out how to do this
IGenericInterface<object> Server {get; set;}
}
如果无法将 GenericTypeProperty
属性 设置为只读,则此方法对您不起作用。无论如何,大多数时候你可以利用方差用构造函数参数替换 setter:
public class ServeRich : IGenericInterface<RichPeople>
{
private readonly RichPeople _people;
public ServerRich(RichPeople people)
{
_people = people;
}
// C# 6 expression-bodied read-only property
public RichPeople GenericTypeProperty => _people;
public void PerformService() { //Serving the rich }
}
抓紧我的秃头....我有一个通用界面
interface IGenericInterface<T>
{
T GenericTypeProperty {get; set;}
void PerformService();
}
我有服务器 classes 实现通用接口并做一些事情
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 }
}
然后我有一个服务 class 如下,我想调用并分配其中一个服务器 class 并调用 PerformService()
public class ServeThem
{
//This is where I am trying to figure out how to do this
IGenericInterface<T<-??> Server {get; set;}
}
我想最终创建服务 class 并在服务器上调用执行服务方法....类似这样
main()
{
ServeThem service= new ServeThem();
service.Server = new ServePoor(); //This will be resolved by Ninject
service.Server.PerformService();
}
我的问题在IGenericInterface<T<-??> Server {get; set;}
。我试图弄清楚如何将 Server
属性 声明为通用接口类型,可以将其分配给实现特定类型接口的 class 对象。似乎为了声明 IGenericInterface<type>
,必须提及一个实际的具体类型。我尝试添加 passedInType
属性 并使用 typeof(passedInType)
但这会引发异常。
如何将 属性 声明为通用接口类型?这可能吗
看来你的服务中没有使用GenericTypeProperty
,所以你可以将PerformService
方法移动到非泛型接口,并在服务中使用此接口class
interface INonGenericInterface
{
void PerformService();
}
interface IGenericInterface<T>: INonGenericInterface
{
T GenericTypeProperty {get; set;}
}
public class ServeThem
{
INonGenericInterface Server {get; set;}
}
您正在寻找 covariance。
// T is covariant (i.e. the "out" keyword)
public interface IGenericInterface<out T>
{
// Check that I removed the setter. T wouldn't be covariantly valid
// if it could be set...
// BTW, this doesn't prevent an implementation to provide a setter.
T GenericTypeProperty { get; }
void PerformService();
}
现在将您的 ServeThem
class 声明为整个 属性 为 IGenericInterface<object>
:
public class ServeThem
{
//This is where I am trying to figure out how to do this
IGenericInterface<object> Server {get; set;}
}
如果无法将 GenericTypeProperty
属性 设置为只读,则此方法对您不起作用。无论如何,大多数时候你可以利用方差用构造函数参数替换 setter:
public class ServeRich : IGenericInterface<RichPeople>
{
private readonly RichPeople _people;
public ServerRich(RichPeople people)
{
_people = people;
}
// C# 6 expression-bodied read-only property
public RichPeople GenericTypeProperty => _people;
public void PerformService() { //Serving the rich }
}