Example #1
0
        /// <summary>
        /// <para>
        /// Scans the IsDirty value of each field and property of type IProperty
        /// </para>
        /// </summary>
        /// <param name="this">The object on which to inspect for dirty properties</param>
        /// <returns>True if any IProperty member's IsDirty value is true, otherwise false</returns>
        public static bool IsDirty(this object @this)// where TOwner : class
        {
            var result     = new Lockable <bool>();
            var fieldInfos = @this.GetType().GetTypeInfo().DeclaredFields;//.GetFields(BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();

            foreach (var fieldInfo in fieldInfos)
            {
                var property = GetProperty(fieldInfo, @this) as IProperty;
                if (property?.IsDirty ?? false)
                {
                    result.Value = true;
                    break;
                }
            }
            if (result.Value)
            {
                return(true);
            }

            var propertyInfos = @this.GetType().GetTypeInfo().DeclaredProperties;//.GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();

            foreach (var propertyInfo in propertyInfos)
            {
                var property = GetProperty(propertyInfo, @this) as IProperty;
                if (property?.IsDirty ?? false)
                {
                    result.Value = true;
                    break;
                }
            }
            return(result.Value);
        }
 /// <summary>
 /// Provides thread-safe assignment of a Lockable object
 /// </summary>
 /// <returns>True if the assignment was made, otherwise false</returns>
 internal static bool LoadValue <TValue>(Lockable <TValue> property, TValue value)
 {
     if (EqualityComparer <TValue> .Default.Equals(property.Value, value))
     {
         return(false);
     }
     property.Value = value;
     return(true);
 }
 /// <summary>
 /// Provides thread-safe assignment of a Lockable object and invoking a handler when the change occurs
 /// </summary>
 /// <returns>True if the assignment was made, otherwise false</returns>
 internal static bool SetValue <TValue>(Lockable <TValue> property, TValue value, Action <TValue, TValue, string> onSetProperty, [CallerMemberName] string propertyName = null)
 {
     if (EqualityComparer <TValue> .Default.Equals(property.Value, value))
     {
         return(false);
     }
     lock (property.LockObject)
     {
         var oldValue = property.UnlockedValue;
         property.UnlockedValue = value;
         onSetProperty?.Invoke(oldValue, value, propertyName);
         return(true);
     }
 }
Example #4
0
        /// <summary>
        /// <para>
        /// Scans the IsDirty value of each field and property of type IProperty
        /// </para>
        /// </summary>
        /// <returns>True if any IProperty member's IsDirty value is true, otherwise false</returns>
        public static bool IsDirty(this object @this)// where TOwner : class
        {
            var result = new Lockable <bool>();

#if WINDOWS_PORTABLE
            var fieldInfos = @this.GetType().GetTypeInfo().DeclaredFields.ToList();
            foreach (var fieldInfo in fieldInfos)
            {
                var property = GetProperty(fieldInfo, @this) as IProperty;
                if (property?.IsDirty ?? false)
                {
                    result.Value = true;
                    break;
                }
            }
#else
            var fieldInfos = @this.GetType().GetFields(BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();
            foreach (var fieldInfo in fieldInfos)
            {
                var property = GetProperty(fieldInfo, @this) as IProperty;
                if (property?.IsDirty ?? false)
                {
                    result.Value = true;
                    break;
                }
            }
            //Parallel.For(0, fieldInfos.Count, (i, state) =>
            //{
            //    var fieldInfo = fieldInfos[i];
            //    var property = GetProperty(fieldInfo, @this) as IProperty;
            //    if (property?.IsDirty ?? false)
            //    {
            //        result.Value = true;
            //        state.Stop();
            //    }
            //});
#endif
            if (result.Value)
            {
                return(true);
            }
#if WINDOWS_PORTABLE
            var propertyInfos = @this.GetType().GetTypeInfo().DeclaredProperties.ToList();
            foreach (var propertyInfo in propertyInfos)
            {
                var property = GetProperty(propertyInfo, @this) as IProperty;
                if (property?.IsDirty ?? false)
                {
                    result.Value = true;
                    break;
                }
            }
#else
            var propertyInfos = @this.GetType().GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();
            foreach (var propertyInfo in propertyInfos)
            {
                var property = GetProperty(propertyInfo, @this) as IProperty;
                if (property?.IsDirty ?? false)
                {
                    result.Value = true;
                    break;
                }
            }
            //Parallel.For(0, propertyInfos.Count, (i, state) =>
            //  {
            //      var propertyInfo = propertyInfos[i];//.GetValue(@this) as IProperty;
            //      var property = GetProperty(propertyInfo, @this) as IProperty;
            //      if (property?.IsDirty ?? false)
            //      {
            //          result.Value = true;
            //          state.Stop();
            //      }
            //  });
#endif
            return(result.Value);
        }