public static T SearchScope <T>(this Scope scope, bool preferDirectParent = false) where T : Scope { if (scope == null) { throw new ArgumentNullException("scope"); } T sought = scope as T; if (sought != null) { return(sought); } BindScope bindScope = scope as BindScope; if (bindScope != null) { sought = SearchScope <T>(preferDirectParent ? bindScope.Parent : bindScope.GrandChild); if (sought != null) { return(sought); } return(SearchScope <T>(preferDirectParent ? bindScope.GrandChild : bindScope.Parent)); } ChildScope childScope = scope as ChildScope; if (childScope != null) { return(SearchScope <T>(childScope.Parent)); } return(null); }
public static T FindScope <T>(this ChildScope scope, bool skipThis = true, bool preferDirectParent = false) where T : Scope { if (scope == null) { throw new ArgumentNullException("scope"); } T sought = SearchScope <T>(skipThis ? scope.Parent : scope, preferDirectParent); if (sought == null) { throw new Exception("Could not find scope"); } return(sought); }
/// <summary> /// Ported from T * search_scope(scope_t * ptr, bool prefer_direct_parents = false) /// </summary> public static T SearchScope <T>(this Scope scope, bool preferDirectParent = false) where T : Scope { if (scope == null) { throw new ArgumentNullException("scope"); } Logger.Current.Debug("scope.search", () => String.Format("Searching scope {0}", scope.Description)); T sought = scope as T; if (sought != null) { return(sought); } BindScope bindScope = scope as BindScope; if (bindScope != null) { sought = SearchScope <T>(preferDirectParent ? bindScope.Parent : bindScope.GrandChild); if (sought != null) { return(sought); } return(SearchScope <T>(preferDirectParent ? bindScope.GrandChild : bindScope.Parent)); } ChildScope childScope = scope as ChildScope; if (childScope != null) { return(SearchScope <T>(childScope.Parent)); } return(null); }