我正在尝试为专有的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
。有人可以建议我如何解决这个问题吗?
当您的库以这种方式返回Java实例时,Xamarin.Android绑定库无法将Java实例映射到其C#接口。
PlzAndroid.Runtime.Extensions.JavaCast<ILocation>(readingInstace)
用于其他类型,依此类推。
可能try-catch
是必需的。
干杯。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句