IfType

public static void IfType<T>(this object item, Action<T> action) where T : class
        {
            if (item is T)
            {
                action(item as T);
            }
        }
Example:
target.IfType<ItemDoesNotExistNavigationTarget>(x => ShowFeedback(x.Message));

the action is only executed when called on a certain type. It Replaces type checking AND casting like this:

if (target is ItemDoesNotExistNavigationTarget){
  ShowFeedback(((ItemDoesNotExistNavigationTarget).Message));
}

Description

Execute code only on certain types

Details

Double click on the code to select all.

 

;