Type cast from Java.Lang.Object to native CLR type in MonoDroid

前端 未结 8 1158
花落未央
花落未央 2021-02-02 13:38

How to cast Java.Lang.Object to some native type?

Example:

ListView adapter contains instances of native type Message. When i am trying to get SelectedItem from

相关标签:
8条回答
  • 2021-02-02 14:04

    it's work for me:

    public class HolderHelper<T> : Java.Lang.Object {
        public readonly T Value;
    
        public HolderHelper (T value)
        {
            this.Value = value;
        }
    }
    

    test:

    chkFileName.Tag = new HolderHelper<LinkInfo> (item);
    LinkInfo link= (chkFileName.Tag as HolderHelper<LinkInfo>).Value;
    
    0 讨论(0)
  • 2021-02-02 14:08

    I used this code from above answer and it works fine to me

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

    and this is how I used

    var selectedLocation = locationSpinner.SelectedItem.Cast<Location>();
    

    I am able to get my location object fine from spinner

    0 讨论(0)
提交回复
热议问题