Example #1
0
        static ScriptBlock GetScriptBlock(string s)
        {
            var sb = ScriptBlock.CreateDelayParsedScriptBlock(s, isProductCode: true);

            sb.LanguageMode = PSLanguageMode.FullLanguage;
            return(sb);
        }
Example #2
0
        /// <summary>
        /// create an expression from an expression token
        /// </summary>
        /// <param name="et">expression token to use</param>
        /// <param name="loadingInfo">The context from which the file was loaded</param>
        /// <returns>constructed expression</returns>
        /// <exception cref="ParseException"></exception>
        internal PSPropertyExpression CreateFromExpressionToken(ExpressionToken et, DatabaseLoadingInfo loadingInfo)
        {
            if (et.isScriptBlock)
            {
                // we cache script blocks from expression tokens
                if (_expressionCache != null)
                {
                    PSPropertyExpression value;
                    if (_expressionCache.TryGetValue(et, out value))
                    {
                        // got a hit on the cache, just return
                        return(value);
                    }
                }
                else
                {
                    _expressionCache = new Dictionary <ExpressionToken, PSPropertyExpression>();
                }

                bool isFullyTrusted = false;
                bool isProductCode  = false;
                if (loadingInfo != null)
                {
                    isFullyTrusted = loadingInfo.isFullyTrusted;
                    isProductCode  = loadingInfo.isProductCode;
                }

                // no hit, we build one and we cache
                ScriptBlock sb = ScriptBlock.CreateDelayParsedScriptBlock(et.expressionValue, isProductCode: isProductCode);
                sb.DebuggerStepThrough = true;

                if (isFullyTrusted)
                {
                    sb.LanguageMode = PSLanguageMode.FullLanguage;
                }

                PSPropertyExpression ex = new PSPropertyExpression(sb);

                _expressionCache.Add(et, ex);

                return(ex);
            }

            // we do not cache if it is just a property name
            return(new PSPropertyExpression(et.expressionValue));
        }