【发布时间】:2018-10-01 05:45:39
【问题描述】:
我正在尝试为专有的 Android 库生成 Xamarin 绑定(换句话说,很遗憾,我无法在此处共享此库)。但是我遇到了多态性的问题。这是情况。
该库公开了 3 个接口 Location、MobilityProfile 和 Trip 都扩展了接口 Reading。
该库还有一个接口Measurement,其中包含Reading getReading(); 方法,该方法应始终返回上述3 个接口之一(Location、MobilityProfile 或Trip)。
我生成了绑定并编译了运行良好的绑定项目。下一步是在我的 Xamarin 项目中使用 Xamarin.Android 绑定,如下所示:
public void ProcessReading(IReading reading)
{
if (reading == null)
return null;
switch (reading)
{
case ILocation location:
// Process location
break;
case IMobilityProfile mobilityProfile:
// Process mobility profile
break;
case ITrip trip:
// Process trip
break;
default:
throw new NotSupportedException($"Processing the type '{reading.GetType().FullName}' is not supported.");
}
}
现在我在default 条件下结束,因为reading 参数的类型是IReadingInvoker。谁能告诉我如何解决这个问题?
【问题讨论】:
标签: c# binding xamarin.android