فهرست منبع

Fix to serialization of columns

Kenric Nugteren 1 سال پیش
والد
کامیت
3c21339a09
2فایلهای تغییر یافته به همراه40 افزوده شده و 6 حذف شده
  1. 35 3
      InABox.Core/Column.cs
  2. 5 3
      InABox.Core/Serialization.cs

+ 35 - 3
InABox.Core/Column.cs

@@ -265,7 +265,7 @@ namespace InABox.Core
         }
     }
 
-    public class Columns<T> : IColumns, IEnumerable<Column<T>>
+    public class Columns<T> : IColumns, ICollection<Column<T>>
     {
         #region Private Fields
 
@@ -279,8 +279,15 @@ namespace InABox.Core
 
         public int Count => columns.Count;
 
+        bool ICollection<Column<T>>.IsReadOnly => false;
+
         #endregion
 
+        private Columns()
+        {
+            columns = new List<Column<T>>();
+        }
+
         public Columns(ColumnTypeFlags flags)
         {
             columns = new List<Column<T>>();
@@ -315,7 +322,7 @@ namespace InABox.Core
                 {
                     var isLocal = !prop.HasParentEntityLink()
                         || (prop.Parent?.HasParentEntityLink() != true && prop.Name.EndsWith(".ID"));
-                    if (flags.HasFlag(ColumnTypeFlags.Local) && !prop.IsCalculated)
+                    if (flags.HasFlag(ColumnTypeFlags.Local) && isLocal && !prop.IsCalculated)
                     {
                         columns.Add(new Column<T>(prop));
                     }
@@ -585,7 +592,7 @@ namespace InABox.Core
 
         #endregion
 
-        #region IEnumerable
+        #region ICollection
 
         public IEnumerator<Column<T>> GetEnumerator()
         {
@@ -599,6 +606,31 @@ namespace InABox.Core
             return columns.GetEnumerator();
         }
 
+        void ICollection<Column<T>>.Add(Column<T> item)
+        {
+            Add(item);
+        }
+
+        void ICollection<Column<T>>.Clear()
+        {
+            columns.Clear();
+        }
+
+        bool ICollection<Column<T>>.Contains(Column<T> item)
+        {
+            return Contains(item.Property);
+        }
+
+        void ICollection<Column<T>>.CopyTo(Column<T>[] array, int arrayIndex)
+        {
+            columns.CopyTo(array, arrayIndex);
+        }
+
+        bool ICollection<Column<T>>.Remove(Column<T> item)
+        {
+            return columns.RemoveAll(x => x.Property == item.Property) > 0;
+        }
+
         #endregion
     }
 

+ 5 - 3
InABox.Core/Serialization.cs

@@ -48,6 +48,8 @@ namespace InABox.Core
                 _serializerSettings.Converters.Add(new ColumnJsonConverter());
                 _serializerSettings.Converters.Add(new SortOrderJsonConverter());
                 _serializerSettings.Converters.Add(new UserPropertiesJsonConverter());
+
+                _serializerSettings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
             }
             _serializerSettings.Formatting = indented ? Formatting.Indented : Formatting.None;
 
@@ -179,7 +181,7 @@ namespace InABox.Core
                     ret = JsonConvert.DeserializeObject<T>(json, settings);
                 }
             }
-            catch (Exception)
+            catch (Exception e)
             {
                 if (strict)
                 {
@@ -191,7 +193,7 @@ namespace InABox.Core
                 }
                 else
                 {
-                    ret = Activator.CreateInstance<T>();
+                    ret = (T)Activator.CreateInstance(typeof(T), true);
                 }
             }
 
@@ -227,7 +229,7 @@ namespace InABox.Core
             }
             catch (Exception)
             {
-                ret = Activator.CreateInstance(T);
+                ret = Activator.CreateInstance(T, true);
             }
 
             return ret;