示例#1
0
        public object VisitConvertExpression(ConvertExpressionAst convertExpressionAst)
        {
            object constantValue;
            if (!CompilingConstantExpression && IsConstantValueVisitor.IsConstant(convertExpressionAst, out constantValue))
            {
                return Expression.Constant(constantValue);
            }

            var typeName = convertExpressionAst.Type.TypeName;
            var hashTableAst = convertExpressionAst.Child as HashtableAst;
            Expression childExpr = null;
            if (hashTableAst != null)
            {
                var temp = NewTemp(typeof(OrderedDictionary), "orderedDictionary");
                if (typeName.FullName.Equals(LanguagePrimitives.OrderedAttribute, StringComparison.OrdinalIgnoreCase))
                {
                    return Expression.Block(typeof(OrderedDictionary),
                        new[] { temp },
                        BuildHashtable(hashTableAst.KeyValuePairs, temp, ordered: true));
                }
                if (typeName.FullName.Equals("PSCustomObject", StringComparison.OrdinalIgnoreCase))
                {
                    // pure laziness here - we should construct the PSObject directly.  Instead, we're relying on the conversion
                    // to create the PSObject from an OrderedDictionary.
                    childExpr = Expression.Block(typeof(OrderedDictionary),
                        new[] { temp },
                        BuildHashtable(hashTableAst.KeyValuePairs, temp, ordered: true));
                }
            }

            if (convertExpressionAst.IsRef())
            {
                var varExpr = convertExpressionAst.Child as VariableExpressionAst;
                if (varExpr != null && varExpr.VariablePath.IsVariable && !varExpr.IsConstantVariable())
                {
                    // We'll wrap the variable in a PSReference, but not the constant variables ($true, $false, $null) because those
                    // can't be changed.
                    IEnumerable<PropertyInfo> unused1;
                    bool unused2;
                    var varType = varExpr.GetVariableType(this, out unused1, out unused2);
                    return Expression.Call(CachedReflectionInfo.VariableOps_GetVariableAsRef,
                                           Expression.Constant(varExpr.VariablePath), _executionContextParameter,
                                           varType != null && varType != typeof(object)
                                               ? Expression.Constant(varType, typeof(Type))
                                               : ExpressionCache.NullType);
                }
            }

            if (childExpr == null)
            {
                childExpr = Compile(convertExpressionAst.Child);
            }

            if (typeName.FullName.Equals("PSCustomObject", StringComparison.OrdinalIgnoreCase))
            {
                // We can't use the normal PSConvertBinder because it is strongly typed, and we
                // play some funny games with the PSCustomObject type (externally, it's just an
                // alias for PSObject, internally we do other stuff with it.)
                return DynamicExpression.Dynamic(PSCustomObjectConverter.Get(), typeof(object), childExpr);
            }

            return ConvertValue(convertExpressionAst.Type, childExpr);
        }
示例#2
0
文件: Compiler.cs 项目: nickchal/pash
 public object VisitConvertExpression(ConvertExpressionAst convertExpressionAst)
 {
     object obj2;
     if (!this.CompilingConstantExpression && IsConstantValueVisitor.IsConstant(convertExpressionAst, out obj2, false, false))
     {
         return Expression.Constant(obj2);
     }
     ITypeName typeName = convertExpressionAst.Type.TypeName;
     HashtableAst child = convertExpressionAst.Child as HashtableAst;
     Expression expression = null;
     if (child != null)
     {
         ParameterExpression temp = this.NewTemp(typeof(OrderedDictionary), "orderedDictionary");
         if (typeName.FullName.Equals("ordered", StringComparison.OrdinalIgnoreCase))
         {
             return Expression.Block(typeof(OrderedDictionary), new ParameterExpression[] { temp }, this.BuildHashtable(child.KeyValuePairs, temp, true));
         }
         if (typeName.FullName.Equals("PSCustomObject", StringComparison.OrdinalIgnoreCase))
         {
             expression = Expression.Block(typeof(OrderedDictionary), new ParameterExpression[] { temp }, this.BuildHashtable(child.KeyValuePairs, temp, true));
         }
     }
     if (convertExpressionAst.IsRef())
     {
         VariableExpressionAst ast2 = convertExpressionAst.Child as VariableExpressionAst;
         if (((ast2 != null) && ast2.VariablePath.IsVariable) && !ast2.IsConstantVariable())
         {
             IEnumerable<PropertyInfo> enumerable;
             bool flag;
             Type type = ast2.GetVariableType(this, out enumerable, out flag);
             return Expression.Call(CachedReflectionInfo.VariableOps_GetVariableAsRef, Expression.Constant(ast2.VariablePath), _executionContextParameter, ((type != null) && !type.Equals(typeof(object))) ? Expression.Constant(type) : ExpressionCache.NullType);
         }
     }
     if (expression == null)
     {
         expression = this.Compile(convertExpressionAst.Child);
     }
     if (typeName.FullName.Equals("PSCustomObject", StringComparison.OrdinalIgnoreCase))
     {
         return Expression.Dynamic(PSCustomObjectConverter.Get(), typeof(object), expression);
     }
     return ConvertValue(typeName, expression);
 }