public static Java.Lang.Object ToNative(Type type, object data) { var dataType = data.GetType(); if (dataType.IsPrimitive || data is string) { if (data is bool b) { return(new Java.Lang.Boolean(b)); } else if (data is float f) { return(new Java.Lang.Float(f)); } else if (data is double d) { return(new Java.Lang.Double(d)); } else if (data is long l) { return(new Java.Lang.Long(l)); } else if (data is int i) { return(new Java.Lang.Integer(i)); } else if (data is string s) { return(new Java.Lang.String(s)); } return((Java.Lang.Object)data); } else if (data is IDictionary dict) { var ret = new JavaDictionary <string, Java.Lang.Object>(); var valueType = DataConverter.GetItemType(dict.GetType(), 1); foreach (var key in dict.Keys) { ret.Add(key as string, ToNative(valueType, dict[key])); } return(ret); } else { var ret = new JavaDictionary <string, Java.Lang.Object>(); foreach (var item in TypePropertyHelper.GetFirebaseProperties(type)) { var prp = item.Value.Property; var key = item.Key; var objData = prp.GetValue(data); if (objData != null) { ret.Add(key, ToNative(prp.PropertyType, objData)); } } return(ret); } //return (Java.Lang.Object) data; }
public static NSObject ToNative <T>(T data) { if (data == null) { return(null); } var type = data.GetType(); if (type.IsPrimitive || data is string) { if (data is T) { return(ToNativePrimitive(data)); } return(new NSObject()); } if (data is IDictionary dict) { var ret = new NSMutableDictionary(); foreach (var key in dict.Keys) { var itemdata = dict[key]; if (itemdata != null) { ret.Add(new NSString(key.ToString()), ToNative(itemdata)); } } return(ret); } else { var ret = new NSMutableDictionary(); foreach (var item in TypePropertyHelper.GetFirebaseProperties(type)) { var prp = item.Value.Property; if (!item.Value.CustomAttribute.ReadOnly) { var key = new NSString(item.Key); var objData = prp.GetValue(data); if (objData != null) { var nativeData = ToNative(objData); ret.Add(key, nativeData); } } } return(ret); } }
internal static IDictionary <string, Java.Lang.Object> ToDictionary <T>(Type type, T data) { if (data is IDictionary dict) { var ret = new Dictionary <string, Java.Lang.Object>(); var valueType = DataConverter.GetItemType(dict.GetType(), 1); foreach (var key in dict.Keys) { ret.Add(key as string, ToNative(valueType, dict[key])); } return(ret); } else { var ret = new Dictionary <string, Java.Lang.Object>(); foreach (var item in TypePropertyHelper.GetFirebaseProperties(type)) { if (!item.Value.CustomAttribute.ReadOnly) { var prp = item.Value.Property; var key = item.Key; var objData = prp.GetValue(data); if (objData != null) { ret.Add(key, ToNative(prp.PropertyType, objData)); } else { ret.Add(key, null); } } } return(ret); } throw new UnknownFormatConversionException("Convert failed, not possible to convert to dictionary"); }
public static object Convert(NSDictionary dict, Type returnType, object objectToModify = null) { var ret = objectToModify ?? Activator.CreateInstance(returnType); if (ret is IDictionary nodeDict) { var type = nodeDict.GetType(); var keyType = GetItemType(type, 0); var dictType = GetItemType(type, 1); var keysToRemove = new List <object>(); foreach (var key in nodeDict.Keys) { keysToRemove.Add(key); } foreach (var itemData in dict) { var key = Convert(itemData.Key, keyType); if (keysToRemove.Contains(key)) { keysToRemove.Remove(key); } if (nodeDict.Contains(key)) { var item = Convert(itemData.Value, dictType, nodeDict[key]); } else { var val = Convert(itemData.Value, dictType); nodeDict.Add(key, val); } } foreach (var keyToRemove in keysToRemove) { if (nodeDict.Contains(keyToRemove)) { nodeDict.Remove(keyToRemove); } } } foreach (var item in TypePropertyHelper.GetFirebaseProperties(returnType)) { var prp = item.Value.Item1; var key = (NSString)item.Key; if (dict.ContainsKey(key)) { var data = dict[key]; //try //{ var internalData = prp.GetValue(ret); prp.SetValue(ret, Convert(data, prp.PropertyType, internalData)); //} //catch (Exception ex) //{ // var i = ex; //} } else { if (prp.PropertyType.IsValueType && prp.PropertyType != typeof(string)) { prp.SetValue(ret, Activator.CreateInstance(prp.PropertyType)); } else { prp.SetValue(ret, null); } } } return(ret); }
public static object Convert(JavaDictionary dict, Type returnType, object objectToModify = null) { var ret = objectToModify ?? Activator.CreateInstance(returnType); if (ret is IDictionary nodeDict) { var type = nodeDict.GetType(); var keyType = GetItemType(type, 0); var dictType = GetItemType(type, 1); var keysToRemove = new List <object>(); foreach (var key in nodeDict.Keys) { keysToRemove.Add(key); } foreach (DictionaryEntry itemData in dict) { var key = Convert(itemData.Key, keyType); keysToRemove.Remove(key); if (nodeDict.Contains(key)) { var item = Convert(itemData.Value, dictType, nodeDict[key]); } else { var val = Convert(itemData.Value, dictType); nodeDict.Add(key, val); } } foreach (var keyToRemove in keysToRemove) { if (nodeDict.Contains(keyToRemove)) { nodeDict.Remove(keyToRemove); } } } foreach (var item in TypePropertyHelper.GetFirebaseProperties(returnType)) { //if (!item.Value.CustomAttribute.ReadOnly) { var prp = item.Value.Property; var key = (string)item.Key; if (dict.Contains(key)) { var data = dict[key]; var internalData = prp.GetValue(ret); var newdata = Convert(data, prp.PropertyType, internalData); try { prp.SetValue(ret, newdata); } catch (System.Exception ex) { var i = ex; } } } } return(ret); }