【发布时间】:2018-04-13 11:25:31
【问题描述】:
我已经创建了界面:
public interface ILocationDetector
{
bool IsChanged { get; }
void Detect(IEnumerable<LocationDto> locations);
}
以及实现接口的抽象类
public abstract class LocationDetector : ILocationDetector
{
public bool IsChanged { get; }
//some other props..
protected LocationDetector(MonitoredLocation monitoredLocation)
{
}
public abstract void Detect(IEnumerable<LocationDto> locations);
//some more methods...
}
我创建了两个从上面派生的类。 我想创建一个符合我的条件的实例。我要做的是:
> ILocationDetector detector = _locationOrigin == LocationOrigin.Native
> ? new ADetector(monitored)
> : new BDetector(monitored);
这给了我一个错误:
无法确定条件表达式的类型,因为存在 第一个和第二个之间没有隐式转换
但是上面的代码可以工作:
ILocationDetector a = new ADetector(monitored);
ILocationDetector b = new BDetector(monitored);
谁能解释一下?
【问题讨论】:
-
它的简单枚举
标签: c#