Exemple #1
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            VisibilityOptions VisibilityOption = parameter == null ? VisibilityOptions.VisibleOnTrue : (VisibilityOptions)Enum.Parse(typeof(VisibilityOptions), parameter.ToString());

            switch (VisibilityOption)
            {
            case VisibilityOptions.VisibleOnFalse:
            case VisibilityOptions.HiddenOnTrue:
            {
                return(value != null ? Visibility.Hidden : Visibility.Visible);
            }

            case VisibilityOptions.CollapsedOnTrue:
            {
                return(value != null ? Visibility.Collapsed : Visibility.Visible);
            }

            case VisibilityOptions.CollapsedOnFalse:
            {
                return(value != null ? Visibility.Visible : Visibility.Collapsed);
            }
            }

            return(value != null ? Visibility.Visible : Visibility.Hidden);
        }
Exemple #2
0
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            bool aggregateBool = false;

            foreach (object value in values)
            {
                aggregateBool |= (bool)value;
            }

            VisibilityOptions VisibilityOption = parameter == null ? VisibilityOptions.VisibleOnTrue : (VisibilityOptions)Enum.Parse(typeof(VisibilityOptions), parameter.ToString());

            switch (VisibilityOption)
            {
            case VisibilityOptions.VisibleOnFalse:
            case VisibilityOptions.HiddenOnTrue:
            {
                return(aggregateBool ? Visibility.Hidden : Visibility.Visible);
            }

            case VisibilityOptions.CollapsedOnTrue:
            {
                return(aggregateBool ? Visibility.Collapsed : Visibility.Visible);
            }

            case VisibilityOptions.CollapsedOnFalse:
            {
                return(aggregateBool ? Visibility.Visible : Visibility.Collapsed);
            }
            }

            return(aggregateBool ? Visibility.Visible : Visibility.Hidden);
        }
Exemple #3
0
 private static bool IsApplicable(FieldInfo field, VisibilityOptions visibility, ResetOptions resetOptions)
 {
     if (visibility == 0)
     {
         throw new ArgumentException("VisibilityOptions are not specified.");
     }
     if (IsBackendField(field))
     {
         var propertyInfo = ExtractPropertyForBackendField(field);
         if (!CheckMethodVisibility(propertyInfo.GetSetMethod(true), visibility))
         {
             return(false);
         }
         if (!CheckNoAutoCleanFilter(propertyInfo, resetOptions))
         {
             return(false);
         }
     }
     else
     {
         if (!CheckFieldVisibility(field, visibility))
         {
             return(false);
         }
         if (!CheckReadonlyFilter(field, resetOptions))
         {
             return(false);
         }
         if (!CheckNoAutoCleanFilter(field, resetOptions))
         {
             return(false);
         }
     }
     return(true);
 }
Exemple #4
0
 private static bool CheckMethodVisibility(MethodInfo method, VisibilityOptions visibilityOptions)
 {
     if (method.IsPublic && visibilityOptions.IsSet(VisibilityOptions.Public))
     {
         return(true);
     }
     if (method.IsFamily && visibilityOptions.IsSet(VisibilityOptions.Protected))
     {
         return(true);
     }
     if (method.IsPrivate && visibilityOptions.IsSet(VisibilityOptions.Private))
     {
         return(true);
     }
     if (method.IsAssembly && visibilityOptions.IsSet(VisibilityOptions.Internal))
     {
         return(true);
     }
     if (method.IsFamilyOrAssembly && visibilityOptions.IsSet(VisibilityOptions.ProtectedInternal))
     {
         return(true);
     }
     if (method.IsFamilyAndAssembly && visibilityOptions.IsSet(VisibilityOptions.ProtectedPrivate))
     {
         return(true);
     }
     return(false);
 }
        public void ResetInstance_should_honor_visibility_options(VisibilityOptions visibilityOptions, bool shouldResetPublic, bool shouldResetProtected, bool shouldResetPrivate, bool shouldResetInternal, bool shouldResetProtectedInternal)
        {
            var current = new Visibility.Current();

            StateCleaner.ResetInstance(current, HierarchyOptions.All, visibilityOptions);

            Assert.That(current.PublicField == null, Is.EqualTo(shouldResetPublic));
            Assert.That(current.PublicPublicProperty == null, Is.EqualTo(shouldResetPublic));

            Assert.That(current.GetProtectedField == null, Is.EqualTo(shouldResetProtected));
            Assert.That(current.PublicProtectedProperty == null, Is.EqualTo(shouldResetProtected));

            Assert.That(current.GetPrivateField == null, Is.EqualTo(shouldResetPrivate));
            Assert.That(current.PublicPrivateProperty == null, Is.EqualTo(shouldResetPrivate));

            Assert.That(current.InternalField == null, Is.EqualTo(shouldResetInternal));
            Assert.That(current.ProtectedInternalField == null, Is.EqualTo(shouldResetProtectedInternal));
        }
 public MyPropertyAttribute(VisibilityOptions visibility)
 {
     this.visibility = visibility;
 }
Exemple #7
0
        /// <summary>
        /// Resets state of specified instance fields/auto-properties to their defaults (null for classes, default value for structs).
        /// If given instance field implements IDisposable interface, it would be disposed before reset.
        ///
        /// It is possible to control which fields/auto-properties would be cleaned with hierarchy, visibility and reset options.
        /// </summary>
        /// <typeparam name="T">Target type.</typeparam>
        /// <param name="target">Target instance to be reset.</param>
        /// <param name="hierarchyOptions">Options controlling which members would be cleaned, depending on which type in type hierarchy they belongs to.</param>
        /// <param name="visibilityOptions">Options controlling which members would be cleaned, depending on their visibility flags.</param>
        /// <param name="resetOptions">An additional reset options.</param>
        public static void ResetInstance <T>(T target, HierarchyOptions hierarchyOptions = HierarchyOptions.All, VisibilityOptions visibilityOptions = VisibilityOptions.All, ResetOptions resetOptions = ResetOptions.None) where T : class
        {
            if (target == null)
            {
                return;
            }

            Debug.WriteLine(string.Format("Resetting instance of: {0}", target.GetType()));
            foreach (var field in GetAllFields(target, typeof(T), hierarchyOptions)
                     .Where(f => IsApplicable(f, visibilityOptions, resetOptions)))
            {
                if ((resetOptions & ResetOptions.DoNotDispose) == 0)
                {
                    DisposeField(field, target);
                }
                ResetField(field, target);
            }
        }
 public VisibilityAttribute(VisibilityOptions visibility)
 {
     VisibilityOnPage = visibility;
 }