Xamarin Android 绑定到继承泛型类型的 Java 类型 [英] Xamarin Android Binding to Java Type which inherits Generic Type
问题描述
我正在寻找解决以下问题的方法.给定 Java 中的类层次结构:
I'm looking for a workaround for the following problem. Given a class hierarchy in Java:
public interface Bar<T> {
getValue<T>();
}
public abstract class BarImpl<T> implements Bar<T> {
}
public class Foo extends BarImpl<Double> {
@override
public Double getValue() {
// ..
}
}
Xamarin.Android 按如下方式生成包装器:
Xamarin.Android generates wrappers as follows:
// Now in C# land
public interface Bar
{
// Xamarin.Android omits T Value { get; }
}
public partial abstract class BarImpl : Bar
{
// Xamarin.Android omits abstract T Value { get; }
}
public partial class Foo : BarImpl
{
// COMPILE ERROR HERE. No method to override
public override java.lang.Double Value
{
get { /* ... */ }
}
}
现在我很清楚Java编译会去除泛型信息,所以这实际上不是一个容易解决的问题,但我确实想知道:
Now I'm well aware that Java compilation strips out generics information, so this isn't actually an easy problem to solve but I do want to know if:
任何人都在 java.android 中遇到过类似的问题
Anyone has come across similar problems in java.android
您使用了哪些巧妙的解决方案来绑定此类层次结构?
What clever solutions you employed to bind this class hierarchy?
我只对 Foo 上的 Value 的 getter(绑定的 c# 类)以及通过基本接口访问它的某种方式感兴趣(cast 是可以的).
I'm only interested in the getter of Value on Foo (bound c# class) and some way to access it via the base interface (cast is OK).
推荐答案
生成器生成部分类.扩展这些可能是一个解决方案.您所要做的就是将文件添加到 Additions
文件夹并将您的内容添加到部分类.我的想法是这样的:
The generator generates partial classes. Extending these could be a solution. All you have to do, is to add files to the Additions
folder and ad your stuff to the partial classes. My idea would look like:
// custom interface
public interface IMyBar<T> : Bar<T>
{
public T Value { get; }
}
// extend the partial base class
public partial abstract class BarImpl : IMyBar<Java.Lang.Double>
{
public abstract Java.Lang.Double Value { get; }
}
// compiler generates the rest
public partial class Foo
{
// COMPILE ERROR HERE. No method to override
public override Java.Lang.Double Value
{
get { /* ... */ }
}
}
这篇关于Xamarin Android 绑定到继承泛型类型的 Java 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!