Browse Source

Fixed coreUtils.ChangeType for dictionaries and other generic collections

Kenric Nugteren 4 days ago
parent
commit
9b52ff8718

+ 12 - 0
InABox.Avalonia/AvaloniaUtils.cs

@@ -112,4 +112,16 @@ public static class AvaloniaUtils
 
     #endregion
 
+    public static T WithClass<T>(this T element, string className)
+        where T : StyledElement
+    {
+        element.Classes.Add(className);
+        return element;
+    }
+    public static T WithClasses<T>(this T element, params string[] classes)
+        where T : StyledElement
+    {
+        element.Classes.AddRange(classes);
+        return element;
+    }
 }

+ 7 - 2
InABox.Avalonia/Converters/ByteArrayToImageSourceConverter.cs

@@ -29,11 +29,16 @@ public class ByteArrayToImageSourceConverter : AbstractConverter<byte[]?, IImage
                 SKData encoded = image.Encode(SKEncodedImageFormat.Png, 100);
                 value = encoded.ToArray();
             }
-            
-            using (var stream = new MemoryStream(value))
+            try
             {
+                using var stream = new MemoryStream(value);
                 result = new Bitmap(stream);
             }
+            catch(Exception e)
+            {
+                MobileLogging.Log(e);
+                return null;
+            }
         }
         return result;
     }

+ 32 - 13
InABox.Core/CoreUtils.cs

@@ -555,13 +555,6 @@ namespace InABox.Core
                 return Enum.ToObject(type, value);
             }
 
-            if (!type.GetTypeInfo().IsInterface && type.GetTypeInfo().IsGenericType)
-            {
-                var innerType = type.GetTypeInfo().GetGenericArguments()[0];
-                var innerValue = ChangeType(value, innerType);
-                return Activator.CreateInstance(type, innerValue);
-            }
-
             if (value is string && type == typeof(Guid))
                 try
                 {
@@ -572,16 +565,19 @@ namespace InABox.Core
                     return Guid.Empty;
                 }
 
-            if (value is IEnumerable<object?> objList)
+            if (value is string && type == typeof(byte[]))
+                return Convert.FromBase64String(value as string);
+
+            if (value is IEnumerable objList)
             {
                 if(type == typeof(Guid))
                 {
-                    return objList.Select(x => ChangeType<Guid>(x)).ToArray();
+                    return objList.Cast<object?>().Select(x => ChangeType<Guid>(x)).ToArray();
                 }
                 else if (type.IsArray)
                 {
                     var elementType = type.GetElementType();
-                    var collection = value as ICollection<object?> ?? objList.ToList();
+                    var collection = value as ICollection<object?> ?? objList.Cast<object?>().ToList();
 
                     var arr = Array.CreateInstance(elementType, collection.Count);
                     var i = 0;
@@ -592,6 +588,32 @@ namespace InABox.Core
                     }
                     return arr;
                 }
+                else if (type.IsGenericType)
+                {
+                    var typeDef = type.GetGenericTypeDefinition();
+                    if(typeDef == typeof(Dictionary<,>))
+                    {
+                        var dict = (Activator.CreateInstance(type) as IDictionary)!;
+                        var dictDef = type.GetSuperclassDefinition(typeof(Dictionary<,>))!;
+                        var keyType = dictDef.GenericTypeArguments[0];
+                        var valType = dictDef.GenericTypeArguments[1];
+                        if(value is IDictionary fromDict)
+                        {
+                            foreach(DictionaryEntry entry in fromDict)
+                            {
+                                dict[ChangeType(entry.Key, keyType)] = ChangeType(entry.Value, valType);
+                            }
+                        }
+                        return dict;
+                    }
+                }
+            }
+
+            if (!type.GetTypeInfo().IsInterface && type.GetTypeInfo().IsGenericType)
+            {
+                var innerType = type.GetTypeInfo().GetGenericArguments()[0];
+                var innerValue = ChangeType(value, innerType);
+                return Activator.CreateInstance(type, innerValue);
             }
 
             if (value is byte[] && type == typeof(Guid))
@@ -615,9 +637,6 @@ namespace InABox.Core
             //if (value is JArray && type == typeof(string[]))
             //    return (value as JArray).Select(x => x.ToString()).ToArray();
 
-            if (value is string && type == typeof(byte[]))
-                return Convert.FromBase64String(value as string);
-
             if(value is FilterConstant constant)
             {
                 if (type == typeof(DateTime))