コード例 #1
0
        internal static object InvokeWithDollarUnderscore(this ScriptBlock scriptBlock, object args)
        {
            var method = scriptBlock.GetType().GetMethod("DoInvokeReturnAsIs", BindingFlags.Instance | BindingFlags.NonPublic);

            var result = method.Invoke(scriptBlock, new[]
            {
                (object)true,                 //useLocalScope
                (object)3,                    //errorhandlingBehavior - WriteToExternalErrorPipe
                (object)args,                 //dollarUnder
                (object)AutomationNull.Value, //input
                (object)AutomationNull.Value, //scriptThis
                (object)new [] { args }       //Args
            });

            if (result == null)
            {
                return(null);
            }

            if (result is PSObject)
            {
                var psObj = (PSObject)result;

                if (psObj.ToString() == string.Empty)
                {
                    return(null);
                }

                var underlying = psObj.BaseObject;

                return(underlying);
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Implements the internal copy of DoInvokeReturnAsIs
        /// </summary>
        /// <param name="ScriptBlock">The scriptblock to execute</param>
        /// <param name="UseLocalScope">Whether a new scope should be created for this</param>
        /// <param name="ErrorHandlingBehavior">How to handle errors. 2 should be the default</param>
        /// <param name="DollerUnder">The value to make available as $_</param>
        /// <param name="Input">The value to make available to $input</param>
        /// <param name="ScriptThis">The value to make available as $this</param>
        /// <param name="Args">The value to make available as $args</param>
        /// <returns>Whatever output this scriptblock generates</returns>
        public static object DoInvokeReturnAsIs(this ScriptBlock ScriptBlock, bool UseLocalScope, int ErrorHandlingBehavior, object DollerUnder, object Input, object ScriptThis, object[] Args)
        {
            object[]   arguments = new object[] { UseLocalScope, ErrorHandlingBehavior, DollerUnder, Input, ScriptThis, Args };
            Type       type      = ScriptBlock.GetType();
            MethodInfo method    = type.GetMethod("DoInvokeReturnAsIs", BindingFlags.NonPublic | BindingFlags.Instance);

            return(method.Invoke(ScriptBlock, arguments));
        }