本文整理汇总了C#中ExecutionContext.SetVariable方法的典型用法代码示例。如果您正苦于以下问题:C# ExecutionContext.SetVariable方法的具体用法?C# ExecutionContext.SetVariable怎么用?C# ExecutionContext.SetVariable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ExecutionContext
的用法示例。
在下文中一共展示了ExecutionContext.SetVariable方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
public override string Execute(ExecutionContext context)
{
string varValue = ExecuteChildren(context);
context.SetVariable(Variable, varValue.Trim(Environment.NewLine.ToCharArray()));
return "";
}
示例2: LoadConfigFile
internal static Hashtable LoadConfigFile(ExecutionContext context, ExternalScriptInfo scriptInfo)
{
object obj2;
object variableValue = context.GetVariableValue(SpecialVariables.PSScriptRootVarPath);
object newValue = context.GetVariableValue(SpecialVariables.PSCommandPathVarPath);
try
{
context.SetVariable(SpecialVariables.PSScriptRootVarPath, Path.GetDirectoryName(scriptInfo.Definition));
context.SetVariable(SpecialVariables.PSCommandPathVarPath, scriptInfo.Definition);
obj2 = PSObject.Base(scriptInfo.ScriptBlock.InvokeReturnAsIs(new object[0]));
}
finally
{
context.SetVariable(SpecialVariables.PSScriptRootVarPath, variableValue);
context.SetVariable(SpecialVariables.PSCommandPathVarPath, newValue);
}
return (obj2 as Hashtable);
}
示例3: ConditionSatisfiedRegex
internal static bool ConditionSatisfiedRegex(bool caseSensitive, object condition, IScriptExtent errorPosition, string str, ExecutionContext context)
{
string str2;
bool success;
RegexOptions options = caseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase;
try
{
Match match;
Regex regex = condition as Regex;
if ((regex != null) && (((regex.Options & RegexOptions.IgnoreCase) != RegexOptions.None) != caseSensitive))
{
match = regex.Match(str);
}
else
{
str2 = PSObject.ToStringParser(context, condition);
match = Regex.Match(str, str2, options);
if (match.Success && (match.Groups.Count > 0))
{
regex = new Regex(str2, options);
}
}
if (match.Success)
{
GroupCollection groups = match.Groups;
if (groups.Count > 0)
{
Hashtable newValue = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
foreach (string str3 in regex.GetGroupNames())
{
Group group = groups[str3];
if (group.Success)
{
int num;
if (int.TryParse(str3, out num))
{
newValue.Add(num, group.ToString());
}
else
{
newValue.Add(str3, group.ToString());
}
}
}
context.SetVariable(SpecialVariables.MatchesVarPath, newValue);
}
}
success = match.Success;
}
catch (ArgumentException exception)
{
str2 = PSObject.ToStringParser(context, condition);
throw InterpreterError.NewInterpreterExceptionWithInnerException(str2, typeof(RuntimeException), errorPosition, "InvalidRegularExpression", ParserStrings.InvalidRegularExpression, exception, new object[] { str2 });
}
return success;
}
示例4: Execute
public override string Execute(ExecutionContext context)
{
var output = new StringBuilder();
object expr = Values.Evaluate(context);
object[] values = expr as object[] ?? new object[] { expr };
foreach(var value in values) {
context.SetVariable(Variable, value);
output.Append(ExecuteChildren(context));
}
return output.ToString();
}
示例5: Evaluation
public void Evaluation()
{
string input = "x${test - 42}.2";
string expected = "x42.2";
var context = new ExecutionContext();
context.SetVariable("test", 84);
var output = new StringBuilder();
using(var reader = new LocatedTextReaderWrapper(input)) {
var parser = new Parser(reader);
foreach(var node in parser.ReadNodes()) {
output.Append(node.Execute(context));
}
Assert.AreEqual(expected, output.ToString());
}
}
示例6: Execute
public override string Execute(ExecutionContext context)
{
var output = new StringBuilder();
double counter = Convert.ToDouble(Start.Evaluate(context), Parser.DefaultCulture);
while(true) {
context.SetVariable(Variable, counter);
output.Append(ExecuteChildren(context));
counter = Convert.ToDouble(context.GetVariable(Variable), Parser.DefaultCulture);
counter += Step == null ? 1.0 : Convert.ToDouble(Step.Evaluate(context), Parser.DefaultCulture);
if(counter >= Convert.ToDouble(End.Evaluate(context), Parser.DefaultCulture)) {
break;
}
}
return output.ToString();
}
示例7: SetPSBoundParametersVariable
internal void SetPSBoundParametersVariable(ExecutionContext context)
{
context.SetVariable(SpecialVariables.PSBoundParametersVarPath, this._dictionary);
}
示例8: EvaluatePowerShellDataFile
//.........这里部分代码省略.........
bool skipPathValidation)
{
if (!skipPathValidation && string.IsNullOrEmpty(parameterName)) { throw PSTraceSource.NewArgumentNullException("parameterName"); }
if (string.IsNullOrEmpty(psDataFilePath)) { throw PSTraceSource.NewArgumentNullException("psDataFilePath"); }
if (context == null) { throw PSTraceSource.NewArgumentNullException("context"); }
string resolvedPath;
if (skipPathValidation)
{
resolvedPath = psDataFilePath;
}
else
{
#region "ValidatePowerShellDataFilePath"
bool isPathValid = true;
// File extension needs to be .psd1
string pathExt = Path.GetExtension(psDataFilePath);
if (string.IsNullOrEmpty(pathExt) ||
!StringLiterals.PowerShellDataFileExtension.Equals(pathExt, StringComparison.OrdinalIgnoreCase))
{
isPathValid = false;
}
ProviderInfo provider;
var resolvedPaths = context.SessionState.Path.GetResolvedProviderPathFromPSPath(psDataFilePath, out provider);
// ConfigPath should be resolved as FileSystem provider
if (provider == null || !Microsoft.PowerShell.Commands.FileSystemProvider.ProviderName.Equals(provider.Name, StringComparison.OrdinalIgnoreCase))
{
isPathValid = false;
}
// ConfigPath should be resolved to a single path
if (resolvedPaths.Count != 1)
{
isPathValid = false;
}
if (!isPathValid)
{
throw PSTraceSource.NewArgumentException(
parameterName,
ParserStrings.CannotResolvePowerShellDataFilePath,
psDataFilePath);
}
resolvedPath = resolvedPaths[0];
#endregion "ValidatePowerShellDataFilePath"
}
#region "LoadAndEvaluatePowerShellDataFile"
object evaluationResult;
try
{
// Create the scriptInfo for the .psd1 file
string dataFileName = Path.GetFileName(resolvedPath);
var dataFileScriptInfo = new ExternalScriptInfo(dataFileName, resolvedPath, context);
ScriptBlock scriptBlock = dataFileScriptInfo.ScriptBlock;
// Validate the scriptblock
scriptBlock.CheckRestrictedLanguage(allowedCommands, allowedVariables, allowEnvironmentVariables);
// Evaluate the scriptblock
object oldPsScriptRoot = context.GetVariableValue(SpecialVariables.PSScriptRootVarPath);
try
{
// Set the $PSScriptRoot before the evaluation
context.SetVariable(SpecialVariables.PSScriptRootVarPath, Path.GetDirectoryName(resolvedPath));
evaluationResult = PSObject.Base(scriptBlock.InvokeReturnAsIs());
}
finally
{
context.SetVariable(SpecialVariables.PSScriptRootVarPath, oldPsScriptRoot);
}
}
catch (RuntimeException ex)
{
throw PSTraceSource.NewInvalidOperationException(
ex,
ParserStrings.CannotLoadPowerShellDataFile,
psDataFilePath,
ex.Message);
}
var retResult = evaluationResult as Hashtable;
if (retResult == null)
{
throw PSTraceSource.NewInvalidOperationException(
ParserStrings.InvalidPowerShellDataFile,
psDataFilePath);
}
#endregion "LoadAndEvaluatePowerShellDataFile"
return retResult;
}
示例9: SetErrorVariables
internal static void SetErrorVariables(IScriptExtent extent, RuntimeException rte, ExecutionContext context, Pipe outputPipe)
{
string newValue = null;
Exception innerException = rte;
int num = 0;
while ((innerException != null) && (num++ < 10))
{
if (!string.IsNullOrEmpty(innerException.StackTrace))
{
newValue = innerException.StackTrace;
}
innerException = innerException.InnerException;
}
context.SetVariable(SpecialVariables.StackTraceVarPath, newValue);
InterpreterError.UpdateExceptionErrorRecordPosition(rte, extent);
ErrorRecord record = rte.ErrorRecord.WrapException(rte);
if (!(rte is PipelineStoppedException))
{
if (outputPipe != null)
{
outputPipe.AppendVariableList(VariableStreamKind.Error, record);
}
context.AppendDollarError(record);
}
}
示例10: SetPreviousFirstLastToken
internal void SetPreviousFirstLastToken(ExecutionContext context)
{
if (this._tokenizer.FirstToken != null)
{
context.SetVariable(SpecialVariables.FirstTokenVarPath, this._previousFirstToken);
if (!Parser.IgnoreTokenWhenUpdatingPreviousFirstLast(this._tokenizer.FirstToken))
{
this._previousFirstToken = this._tokenizer.FirstToken.Text;
}
context.SetVariable(SpecialVariables.LastTokenVarPath, this._previousLastToken);
if (!Parser.IgnoreTokenWhenUpdatingPreviousFirstLast(this._tokenizer.LastToken))
{
this._previousLastToken = this._tokenizer.LastToken.Text;
}
}
}
示例11: Execute
public override string Execute(ExecutionContext context)
{
context.SetVariable(Variable, new ExecutionContext.OsqFunction((token, subContext) => {
subContext.PushScope();
try {
var parameters = token == null ? new TokenNode[] { } : token.ChildrenTokenNodes;
if(parameters.Count == 1 && parameters[0].Token.IsSymbol(",")) {
parameters = parameters[0].ChildrenTokenNodes;
}
var parameterVariables = FunctionParameters ?? new string[] { };
using(var paramNameEnumerator = parameterVariables.GetEnumerator()) {
foreach(var child in parameters) {
if(!paramNameEnumerator.MoveNext()) {
break;
}
object value = child.Evaluate(context);
subContext.SetLocalVariable(paramNameEnumerator.Current, value);
}
}
string output = ExecuteChildren(context);
return output.TrimEnd(Environment.NewLine.ToCharArray());
} finally {
subContext.PopScope();
}
}));
return "";
}
示例12: SetPreviousFirstLastToken
internal void SetPreviousFirstLastToken(ExecutionContext context)
{
var firstToken = _tokenizer.FirstToken;
if (firstToken != null)
{
context.SetVariable(SpecialVariables.FirstTokenVarPath, _previousFirstTokenText);
if (!IgnoreTokenWhenUpdatingPreviousFirstLast(firstToken))
{
var stringToken = firstToken as StringToken;
_previousFirstTokenText = stringToken != null
? stringToken.Value
: firstToken.Text;
}
context.SetVariable(SpecialVariables.LastTokenVarPath, _previousLastTokenText);
var lastToken = _tokenizer.LastToken;
if (!IgnoreTokenWhenUpdatingPreviousFirstLast(lastToken))
{
var stringToken = lastToken as StringToken;
_previousLastTokenText = stringToken != null
? stringToken.Value
: lastToken.Text;
}
}
}