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

前端 未结 8 1161
花落未央
花落未央 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:08

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

        public static class ObjectTypeHelper
    {
        public static T Cast(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();
    

    I am able to get my location object fine from spinner

提交回复
热议问题