Beispiel #1
0
        internal static PSReference GetVariableAsRef(VariablePath variablePath, ExecutionContext executionContext, Type staticType)
        {
            Diagnostics.Assert(variablePath.IsVariable, "calller to verify varpath is a variable.");

            SessionStateInternal sessionState = executionContext.EngineSessionState;
            CommandOrigin        origin       = sessionState.CurrentScope.ScopeOrigin;

            SessionStateScope scope;
            PSVariable        var = sessionState.GetVariableItem(variablePath, out scope, origin);

            if (var == null)
            {
                throw InterpreterError.NewInterpreterException(variablePath, typeof(RuntimeException), null,
                                                               "NonExistingVariableReference",
                                                               ParserStrings.NonExistingVariableReference);
            }

            object value = var.Value;

            if (staticType == null && value != null)
            {
                value = PSObject.Base(value);
                if (value != null)
                {
                    staticType = value.GetType();
                }
            }
            if (staticType == null)
            {
                var declaredType = var.Attributes.OfType <ArgumentTypeConverterAttribute>().FirstOrDefault();
                staticType = declaredType != null ? declaredType.TargetType : typeof(LanguagePrimitives.Null);
            }

            return(PSReference.CreateInstance(var, staticType));
        }
Beispiel #2
0
        private static PSReference GetVariableAsRef(VariablePath variablePath, ExecutionContext executionContext, Type staticType)
        {
            SessionStateScope    scope;
            SessionStateInternal engineSessionState = executionContext.EngineSessionState;
            CommandOrigin        scopeOrigin        = engineSessionState.CurrentScope.ScopeOrigin;
            PSVariable           variable           = engineSessionState.GetVariableItem(variablePath, out scope, scopeOrigin);

            if (variable == null)
            {
                throw InterpreterError.NewInterpreterException(variablePath, typeof(RuntimeException), null, "NonExistingVariableReference", ParserStrings.NonExistingVariableReference, new object[0]);
            }
            object obj2 = variable.Value;

            if ((staticType == null) && (obj2 != null))
            {
                obj2 = PSObject.Base(obj2);
                if (obj2 != null)
                {
                    staticType = obj2.GetType();
                }
            }
            if (staticType == null)
            {
                ArgumentTypeConverterAttribute attribute = variable.Attributes.OfType <ArgumentTypeConverterAttribute>().FirstOrDefault <ArgumentTypeConverterAttribute>();
                staticType = (attribute != null) ? attribute.TargetType : typeof(LanguagePrimitives.Null);
            }
            return(PSReference.CreateInstance(variable, staticType));
        }