public T Get(string name, bool isQualified) { if (name == null) { throw new MethodInvocationException("The value of the argument \"name\" is null"); } //if the name is qualified, look for the specified scope if (isQualified) { var qualName = new QualifiedName(name); if (!String.IsNullOrEmpty(qualName.ScopeSpecifier)) { SessionStateScope <T> affectedScope = GetScope(qualName.ScopeSpecifier, false); var item = affectedScope.GetLocal(qualName.UnqualifiedName); //return null if it's private if (affectedScope != this && item.ItemOptions.HasFlag(ScopedItemOptions.Private)) { return(default(T)); } return(item); } } //no scope specifier in name, look in hierarchy var hostingScope = this.FindHostingScope(name); if (hostingScope != null) { return(hostingScope.GetLocal(name)); } return(default(T)); }
public IEnumerable <string> Find(string pattern, bool isQualified) { if (pattern == null) { throw new MethodInvocationException("The value of the argument \"pattern\" is null"); } //if the name is qualified, look for the specified scope if (isQualified) { var qualName = new QualifiedName(pattern); if (!String.IsNullOrEmpty(qualName.ScopeSpecifier)) { SessionStateScope <T> affectedScope = GetScope(qualName.ScopeSpecifier, false); var wildcard = new WildcardPattern(qualName.UnqualifiedName, WildcardOptions.IgnoreCase); return(from pair in affectedScope.Items where wildcard.IsMatch(pair.Key) && (affectedScope == this || !pair.Value.ItemOptions.HasFlag(ScopedItemOptions.Private)) select qualName.ScopeSpecifier + ":" + pair.Key); } } var wildcardPattern = new WildcardPattern(pattern, WildcardOptions.IgnoreCase); var visibleItems = new List <string>(); //now check recursively the parent scopes for non-private, not overriden variables foreach (var curScope in new ScopeHierarchyIterator(this)) { visibleItems.AddRange(from pair in curScope.Items where wildcardPattern.IsMatch(pair.Key) && !visibleItems.Contains(pair.Key) && (!pair.Value.ItemOptions.HasFlag(ScopedItemOptions.Private) || curScope == this) select pair.Key); } return(visibleItems); }
public SessionStateScope(SessionStateScope <T> parentItems, SessionStateCategory sessionStateCategory) { ParentScope = parentItems; Items = new Dictionary <string, T>(StringComparer.CurrentCultureIgnoreCase); //TODO: care about AllScope items! SessionStateCategory = sessionStateCategory; }
public void Set(string name, ScriptBlock function, string description = "") { var qualName = new SessionStateScope <FunctionInfo> .QualifiedName(name); var info = new FunctionInfo(qualName.UnqualifiedName, function); info.Description = description; _scope.Set(name, info, true, true); }
public void Set(string name, ScriptBlock function, IEnumerable <ParameterAst> parameters, string description) { var qualName = new SessionStateScope <FunctionInfo> .QualifiedName(name); var info = new FunctionInfo(qualName.UnqualifiedName, function, parameters); info.Description = description; Scope.Set(name, info, true, true); }
public void Remove(string name, bool isQualified) { if (name == null) { throw new MethodInvocationException("The value of the argument \"name\" is null"); } //if the scope isn't specified, we will look in the hierarchy for the variable SessionStateScope <T> affectedScope = null; if (isQualified) { var qualName = new QualifiedName(name); affectedScope = GetScope(qualName.ScopeSpecifier, false); name = qualName.UnqualifiedName; //use the unqualified name to set the item } if (affectedScope == null) { affectedScope = FindHostingScope(name) ?? this; } affectedScope.RemoveLocal(name); }
private SessionStateScope <T> GetScope(string specifier, bool numberAllowed, SessionStateScope <T> fallback = null) { int scopeLevel = -1; if (String.IsNullOrEmpty(specifier)) { return(fallback); } SessionStateScope <T> candidate = this; //check if the specifier is a number before trying to interprete it as enum if (int.TryParse(specifier, out scopeLevel)) { //sometimes a number specifies the relative scope location, but in some contexts this isn't allowed //this has to happen *after* trying to parse them as e.g. "0" would be a parsable enum value! if (!numberAllowed || scopeLevel < 0) { throw new ArgumentException("Invalid scope specifier"); } for (var curLevel = scopeLevel; curLevel > 0; curLevel--) { //make sure we can proceed looking for the next scope candidate = candidate.ParentScope; if (candidate == null) { throw new ArgumentOutOfRangeException("Exceeded the maximum number of available scopes"); } } return(candidate); } //it's not a (positive) number, let's check if it's a named scope ScopeSpecifiers scopeSpecifier; if (!ScopeSpecifiers.TryParse(specifier, true, out scopeSpecifier)) { throw new ArgumentException(String.Format("Invalid scope specifier \"{0}\".", specifier)); } return(GetScope(scopeSpecifier)); }
internal AliasIntrinsics(SessionState sessionState, SessionStateScope <AliasInfo> aliasScope) { _sessionState = sessionState; _scope = aliasScope; }
public ScopeHierarchyIterator(SessionStateScope <T> start) { _start = start; }
public SessionStateIntrinsics(SessionStateScope <T> scope, bool supportsScopedName) { Scope = scope; SupportsScopedName = supportsScopedName; }
internal FunctionIntrinsics(SessionStateScope <FunctionInfo> functionScope) { _scope = functionScope; }
internal CmdletIntrinsics(SessionStateScope <CmdletInfo> scope) : base(scope, false) { }
internal AliasIntrinsics(SessionStateScope <AliasInfo> scope) : base(scope, false) { }
public ModuleIntrinsics(SessionStateScope <PSModuleInfo> scope) : base(scope, false) { }
internal FunctionIntrinsics(SessionStateScope <FunctionInfo> scope) : base(scope, true) { }