Example #1
0
 /// <summary>
 /// Adds a default label to a <see cref="TypeSwitch"/>.
 /// </summary>
 /// <param name="typeSwitch">The current <see cref="TypeSwitch"/>.</param>
 /// <param name="a">The action to perform if <see cref="TypeSwitch.Object"/> reaches the Default case.</param>
 /// <returns>The current TypeSwitch to be used with chaining cases.</returns>
 public static void Default(this TypeSwitch typeSwitch, Action <object> a)
 {
     if (typeSwitch != null)
     {
         a(typeSwitch.Object);
     }
 }
Example #2
0
        /// <summary>
        /// Adds a case label to a <see cref="TypeSwitch"/>.
        /// </summary>
        /// <typeparam name="T">The case type.</typeparam>
        /// <param name="typeSwitch">The current <see cref="TypeSwitch"/>.</param>
        /// <param name="c">A filter function that returns <c>true</c> if the object should run the case; otherwise <c>false</c>.</param>
        /// <param name="a">The action to perform if <see cref="TypeSwitch.Object"/> matches the case type.</param>
        /// <param name="fallThrough"><c>true</c> to fall-through to any following cases; otherwise <c>false</c>.</param>
        /// <returns>The current TypeSwitch to be used with chaining cases.</returns>
        public static TypeSwitch Case <T>(this TypeSwitch typeSwitch, Func <T, bool> c, Action <T> a, bool fallThrough)
        {
            if (typeSwitch == null)
            {
                return(null);
            }

            var typedObject = typeSwitch.Object is T ? (T)typeSwitch.Object : default(T);
            var type        = typeSwitch.Type;
            var caseType    = typeof(T);

            if ((typedObject == null || typedObject.Equals(default(T)) || !c(typedObject) || type != null && !Device.Reflector.IsAssignableFrom(caseType, type)) &&
                (type == null || !Device.Reflector.IsAssignableFrom(caseType, type) || !c(default(T))))
            {
                return(typeSwitch);
            }
            a(typedObject);
            return(fallThrough ? typeSwitch : null);
        }
Example #3
0
 /// <summary>
 /// Adds a case label to a <see cref="TypeSwitch"/>.
 /// </summary>
 /// <typeparam name="T">The case type.</typeparam>
 /// <param name="typeSwitch">The current <see cref="TypeSwitch"/>.</param>
 /// <param name="c">A filter function that returns <c>true</c> if the object should run the case; otherwise <c>false</c>.</param>
 /// <param name="a">The action to perform if <see cref="TypeSwitch.Object"/> matches the case type.</param>
 /// <returns>The current TypeSwitch to be used with chaining cases.</returns>
 public static TypeSwitch Case <T>(this TypeSwitch typeSwitch, Func <T, bool> c, Action <T> a)
 {
     return(Case(typeSwitch, c, a, false));
 }
Example #4
0
 /// <summary>
 /// Adds a case label to a <see cref="TypeSwitch"/>.
 /// </summary>
 /// <typeparam name="T">The case type.</typeparam>
 /// <param name="typeSwitch">The current <see cref="TypeSwitch"/>.</param>
 /// <param name="a">The action to perform if <see cref="TypeSwitch.Object"/> matches the case type.</param>
 /// <param name="fallThrough"><c>true</c> to fall-through to any following cases; otherwise <c>false</c>.</param>
 /// <returns>The current TypeSwitch to be used with chaining cases.</returns>
 public static TypeSwitch Case <T>(this TypeSwitch typeSwitch, Action <T> a, bool fallThrough)
 {
     return(Case(typeSwitch, o => true, a, fallThrough));
 }
Example #5
0
 /// <summary>
 /// Adds a case label to a <see cref="TypeSwitch"/>.
 /// </summary>
 /// <typeparam name="T">The case type.</typeparam>
 /// <param name="typeSwitch">The current <see cref="TypeSwitch"/>.</param>
 /// <param name="a">The action to perform if <see cref="TypeSwitch.Object"/> matches the case type.</param>
 /// <returns>The current TypeSwitch to be used with chaining cases.</returns>
 public static TypeSwitch Case <T>(this TypeSwitch typeSwitch, Action <T> a)
 {
     return(Case(typeSwitch, o => true, a, false));
 }