Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        /// <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);
        }