Beispiel #1
0
        public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
        {
            BindingRestrictions restrictions;
            PSMemberInfo        info;
            bool flag;
            Type type;

            if (!target.HasValue)
            {
                return(base.Defer(target, new DynamicMetaObject[0]));
            }
            object obj2 = PSObject.Base(target.Value);

            if (obj2 == null)
            {
                return(this.PropertyDoesntExist(target, target.PSGetTypeRestriction()).WriteToDebugLog(this));
            }
            Expression expr = null;

            if (this._hasInstanceMember && TryGetInstanceMember(target.Value, base.Name, out info))
            {
                ParameterExpression expression2 = Expression.Variable(typeof(PSMemberInfo));
                expr = WrapGetMemberInTry(Expression.Condition(Expression.Call(CachedReflectionInfo.PSGetMemberBinder_TryGetInstanceMember, target.Expression.Cast(typeof(object)), Expression.Constant(base.Name), expression2), Expression.Property(expression2, "Value"), base.GetUpdateExpression(typeof(object))));
                return(new DynamicMetaObject(Expression.Block(new ParameterExpression[] { expression2 }, new Expression[] { expr }), BinderUtils.GetVersionCheck(this, this._version)).WriteToDebugLog(this));
            }
            info = this.GetPSMemberInfo(target, out restrictions, out flag, out type, null, null);
            if (!flag)
            {
                return(new DynamicMetaObject(WrapGetMemberInTry(Expression.Call(CachedReflectionInfo.PSGetMemberBinder_GetAdaptedValue, GetTargetExpr(target).Cast(typeof(object)), Expression.Constant(base.Name))), restrictions).WriteToDebugLog(this));
            }
            if (info != null)
            {
                PSPropertyInfo info2 = info as PSPropertyInfo;
                if (info2 != null)
                {
                    if (!info2.IsGettable)
                    {
                        return(new DynamicMetaObject(Compiler.ThrowRuntimeError("WriteOnlyProperty", ExtendedTypeSystem.WriteOnlyProperty, this.ReturnType, new Expression[] { Expression.Constant(base.Name) }), restrictions).WriteToDebugLog(this));
                    }
                    PSProperty property = info2 as PSProperty;
                    if (property != null)
                    {
                        DotNetAdapter.PropertyCacheEntry adapterData = property.adapterData as DotNetAdapter.PropertyCacheEntry;
                        if (!adapterData.member.DeclaringType.IsGenericTypeDefinition)
                        {
                            Expression   expression = this._static ? null : GetTargetExpr(target);
                            PropertyInfo member     = adapterData.member as PropertyInfo;
                            if (member != null)
                            {
                                expr = Expression.Property(expression, member);
                            }
                            else
                            {
                                expr = Expression.Field(expression, (FieldInfo)adapterData.member);
                            }
                        }
                        else
                        {
                            expr = ExpressionCache.NullConstant;
                        }
                    }
                    PSScriptProperty property2 = info2 as PSScriptProperty;
                    if (property2 != null)
                    {
                        expr = Expression.Call(Expression.Constant(property2), CachedReflectionInfo.PSScriptProperty_InvokeGetter, new Expression[] { target.Expression.Cast(typeof(object)) });
                    }
                    PSCodeProperty property3 = info2 as PSCodeProperty;
                    if (property3 != null)
                    {
                        expr = PSInvokeMemberBinder.InvokeMethod(property3.GetterCodeReference, null, new DynamicMetaObject[] { target }, false);
                    }
                    if (info2 is PSNoteProperty)
                    {
                        expr = Expression.Property(Expression.Constant(info2), CachedReflectionInfo.PSNoteProperty_Value);
                    }
                    if (type != null)
                    {
                        expr = expr.Convert(type);
                    }
                }
                else
                {
                    expr = Expression.Call(CachedReflectionInfo.PSGetMemberBinder_CloneMemberInfo, Expression.Constant(info).Cast(typeof(PSMemberInfo)), target.Expression.Cast(typeof(object)));
                }
            }
            if (obj2 is IDictionary)
            {
                Type genericTypeArg = null;
                bool flag2          = IsGenericDictionary(obj2, ref genericTypeArg);
                if (!flag2 || (genericTypeArg != null))
                {
                    ParameterExpression ifTrue = Expression.Variable(typeof(object));
                    if (expr == null)
                    {
                        expr = (errorSuggestion ?? this.PropertyDoesntExist(target, restrictions)).Expression;
                    }
                    MethodInfo method = flag2 ? CachedReflectionInfo.PSGetMemberBinder_TryGetGenericDictionaryValue.MakeGenericMethod(new Type[] { genericTypeArg }) : CachedReflectionInfo.PSGetMemberBinder_TryGetIDictionaryValue;
                    expr = Expression.Block(new ParameterExpression[] { ifTrue }, new Expression[] { Expression.Condition(Expression.Call(method, GetTargetExpr(target).Cast(method.GetParameters()[0].ParameterType), Expression.Constant(base.Name), ifTrue), ifTrue, expr.Cast(typeof(object))) });
                }
            }
            if (expr != null)
            {
                return(new DynamicMetaObject(WrapGetMemberInTry(expr), restrictions).WriteToDebugLog(this));
            }
            return((errorSuggestion ?? this.PropertyDoesntExist(target, restrictions)).WriteToDebugLog(this));
        }
Beispiel #2
0
 private string ConvertArrayToString <T>(PSProperty prop)
 {
     T[]      aryv   = (T[])prop.Value;
     string[] result = aryv.Select(x => x.ToString()).ToArray();
     return($"{{\"{string.Join("\",\"", result)}\"}}");
 }