/// <summary> /// handles an item being deleted. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Item_Deleted(object sender, DirectoryItemDeletedEventArgs e) { // unregister the item. var currentMode = _visitMode; _visitMode = VisitMode.Unregister; e.DeletedItem.Accept(this); _visitMode = currentMode; OnRaiseItemDeleted(e); }
// private string _pattern; /// <summary> /// Constructs a new instance of a directory watcher that can be used to subscribe to events that signal when changes are made to a directory. /// </summary> /// <param name="autoWatchNewSubFolders">If true, will automatically Watch any new subfolders that happen to be created in any directory currently being watched.</param> public DirectoryWatcher(IDirectory directory) { // _autoWatchNewSubFolders = autoWatchNewSubFolders; _watchingFolders = new ConcurrentDictionary <string, IDirectoryItem>(); _directory = directory; _Filters = new List <Glob>(); _visitMode = VisitMode.Register; _directory.Accept(this); // visit all items in the directory and attach handlers for event notifications. // _pattern = pattern; }
/// <summary> /// Handles an item being added to a folder that is being watched. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Folder_ItemAdded(object sender, DirectoryItemAddedEventArgs e) { // register the new item. var currentMode = _visitMode; _visitMode = VisitMode.Register; e.NewItem.Accept(this); _visitMode = currentMode; OnRaiseItemAdded(e); }
/// <summary> /// Unregisters all directories and files, unregistering event handlers. /// </summary> /// <returns></returns> private bool UnregisterAll() { List <string> invalidKeys = new List <string>(); var keys = _watchingFolders.Keys.ToArray(); var currentMode = _visitMode; _visitMode = VisitMode.Unregister; foreach (var key in keys) { IDirectoryItem watchedFolder; if (_watchingFolders.TryGetValue(key, out watchedFolder)) { _UnregisterWasSuccessful = false; watchedFolder.Accept(this); if (!_UnregisterWasSuccessful) { // can't stop watching this folder, perhaps it was removed by something else? invalidKeys.Add(key); continue; } } else { // can't get this value, perhaps another thread has removed it? invalidKeys.Add(key); continue; } } _visitMode = currentMode; if (invalidKeys.Any()) { return(false); } return(true); }
public static Dictionary <Type, MethodInfo> FindMatchingExtensionMethods(Func <MethodInfo, bool> filterMethods, Func <MethodInfo, Type> keySelector, VisitMode mode = VisitMode.OnlyClassesWithAttribute) { return(mode == VisitMode.OnlyClassesWithAttribute ? FindMatchingExtensionMethods <GraphtoolsExtensionMethodsAttribute>(filterMethods, keySelector) // only goes through methods inside a class with GraphtoolsExtensionMethodsAttribute : FindMatchingExtensionMethods <ExtensionAttribute>(filterMethods, keySelector)); // goes through every method. Super slow. Kept for test purposes. }
static void VisitProperties(object instance, VisitMode mode, object firstVersion) { if (instance == null) { return; } if (mode == VisitMode.Restore && firstVersion == null) { return; } var enumerable = instance as IEnumerable; if (enumerable != null) { return; } var type = instance.GetType(); if (!type.IsClass) { return; } foreach (var propertyInfo in type.GetProperties()) { if (!(propertyInfo.CanWrite && propertyInfo.CanRead)) { continue; } var customAttributes = propertyInfo.GetCustomAttributes(typeof(JsonIgnoreSerializationOnPostOperationAttribute), false); if (customAttributes.Any()) { if (mode == VisitMode.Clean) { propertyInfo.SetValue(instance, propertyInfo.PropertyType.GetDefaultValue()); continue; } if (mode == VisitMode.Restore) { var value = propertyInfo.GetValue(instance); var defaultValue = propertyInfo.PropertyType.GetDefaultValue(); if (value == defaultValue || value.Equals(defaultValue)) { propertyInfo.SetValue(instance, propertyInfo.GetValue(firstVersion)); } continue; } throw new InvalidOperationException(mode.ToString()); } var propertyType = propertyInfo.PropertyType; if (propertyType.IsNumeric() || propertyType == typeof(string)) { continue; } if (mode == VisitMode.Clean) { VisitProperties(propertyInfo.GetValue(instance), mode, null); continue; } if (mode == VisitMode.Restore) { VisitProperties(propertyInfo.GetValue(instance), mode, propertyInfo.GetValue(firstVersion)); continue; } throw new InvalidOperationException(mode.ToString()); } }
public TreeVisitor(Tree <TItem> tree, VisitMode visitMode) { _tree = tree; _visitMode = visitMode; }
public RelayVisitorExceptionHandledEventArgs(Exception ex, VisitMode mode) : base() { WalkerMode = mode; }