简体   繁体   English

将强制类型的Java Object输入我的班级

[英]Type cast Java Object to my class

Developing application in C# .Net 4.5 using Xamarin, targeting Android. 使用Xamarin针对Android开发C#.Net 4.5中的应用程序。 I have a custom class that has some properties in it. 我有一个自定义类,其中包含一些属性。 I am trying to use a built in component that does comparison using Java.Util.IComparator and Java.Lang.Object. 我正在尝试使用一个内置组件,该组件使用Java.Util.IComparator和Java.Lang.Object进行比较。 Because it is a built in component, I don't have much flexibility into changing those two items. 因为它是一个内置组件,所以我在更改这两项时没有太多的灵活性。

My custom class is named recBatch and inside of it, I have some properties of integers and strings. 我的自定义类名为recBatch,在它的内部,我具有一些整数和字符串属性。

This is where the component gets initialized. 这是组件初始化的地方。 It basically calls a method each time the user clicks on the header for column 0. 基本上,每次用户单击列0的标题时,它都会调用一个方法。

tableView.SetColumnComparator(0, GetBatchIdComparator());

This is the method that gets called by the component 这是组件调用的方法

public Java.Util.IComparator GetBatchIdComparator() { return new BatchIdComparator(); }

And finally, here is the class that is returned by the call. 最后,这是调用返回的类。

public class BatchIdComparator : Java.Lang.Object, Java.Util.IComparator
{


    public int Compare(Java.Lang.Object lhs, Java.Lang.Object rhs)
    {
        var leftID = (recBatch)lhs;
        var rightID = (recBatch)rhs;

        return leftID.Batch.CompareTo(rightID.Batch);
    }


}

The first thing I tried to do above by just casting gives me an error as seen here. 我上面仅通过强制转换尝试做的第一件事给了我一个错误,如下所示。 I did try what Visual Studio is suggesting but could not get it working either. 我确实尝试了Visual Studio的建议,但也无法使其正常工作。

错误提示无法从对象投射到recBatch

The next thing I tried was to create a new class like this one and change the cast from recBatch, my actual class to this new class to do the casting: 我试图做的下一件事是创建一个像这样的新类,并将类型从我的实际类recBatch更改为这个新类以进行铸造:

 public class BatchIdComparator : Java.Lang.Object, Java.Util.IComparator

 {


    public int Compare(Java.Lang.Object lhs, Java.Lang.Object rhs)
    {
        var leftID = (castClass)lhs;
        var rightID = (castClass)rhs;

        return leftID.BatchData.Batch.CompareTo(rightID.BatchData.Batch);
    }


}

public class castClass : Java.Lang.Object
{
    public castClass(recBatch batchData)
    {
        batchData = BatchData;
    }

    public recBatch BatchData { get; private set; }
}

With this, I don't have errors and can compile but the problem is I am getting a cast exception when I run. 这样,我没有错误并且可以编译,但是问题是我在运行时遇到强制转换异常。 The code does compile and because I am casting, I do have access to one of the properties in recBatch (Batch or recBatch.Batch). 该代码确实可以编译,并且由于我正在强制转换,因此可以访问recBatch中的属性之一(Batch或recBatch.Batch)。 However, again, I get a cast exception. 但是,我再次遇到强制转换异常。 The exact error is: 确切的错误是:

强制异常

So basically, I just need to cast the Java.Lang.Object into recBatch but I guess I am doing it wrong. 因此,基本上,我只需要将Java.Lang.Object转换为recBatch,但我想我做错了。 Everything is "wired up" properly because if I put a break point at the Compare method, it does hit and the lhs, rhs arguments that are passed in have my class data in them (ie Batch) even though they are Java.Lang.Object types. 一切都“正确连接”,因为如果我在Compare方法上设置一个断点,它会命中,并且传入的lhs,rhs参数即使在它们是Java.Lang的情况下,也都在其中包含了我的类数据(即Batch)。对象类型。

Any help is appreciated! 任何帮助表示赞赏!

Thanks! 谢谢!

All, for those who may be interested, I have a solution to this that is in place and working quite well. 所有人,对于可能有兴趣的人,我有一个适当的解决方案,并且运行良好。 It took another day of searching after some hints in my comments and some other clues. 在搜寻了我的评论中的一些提示和其他线索后,又花了一天的时间进行搜索。

Basically, I created another class that is solely responsible for casting my Java object. 基本上,我创建了另一个类,该类仅负责转换Java对象。 May not have needed to do this in a class but oh well. 可能不需要在课堂上做到这一点,但是很好。 Here is the class and method contained inside: 这是其中包含的类和方法:

public class CastJavaObject
{
    public static T Cast<T>(Java.Lang.Object obj) where T : recBatch
    {
        var propInfo = obj.GetType().GetProperty("Instance");
        return propInfo == null ? null : propInfo.GetValue(obj, null) as T;
    }
}

Then, all I had to do was call it and pass in the Java object and just like that, the lhsCopy and rhsCopy were of my class, recBatch and not the Java Object and therefore, I could access all of the properties. 然后,我要做的就是调用它并传入Java对象,就像这样,lhsCopy和rhsCopy是我的类,recBatch而不是Java Object的,因此,我可以访问所有属性。 I don't get any exceptions or notice any performance issues. 我没有任何例外或注意到任何性能问题。 However, if somebody has some comments on this approach, please feel free. 但是,如果有人对此方法有任何意见,请放心。

Here is how I called it: 这是我的称呼:

    public int Compare(Java.Lang.Object lhs, Java.Lang.Object rhs)
    {
        var lhsCopy = CastJavaObject.Cast<recBatch>(lhs);
        var rhsCopy = CastJavaObject.Cast<recBatch>(rhs);

Thanks! 谢谢!

Mike 麦克风

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM