private DynamicMetaObject IndexWithNegativeChecks(DynamicMetaObject target, DynamicMetaObject index, DynamicMetaObject value, PropertyInfo lengthProperty, Func <Expression, Expression, Expression, Expression> generateIndexOperation) { DynamicMetaObject[] dynamicMetaObjectArray = new DynamicMetaObject[1]; dynamicMetaObjectArray[0] = index; BindingRestrictions bindingRestriction = target.CombineRestrictions(dynamicMetaObjectArray).Merge(value.Restrictions); bindingRestriction = bindingRestriction.Merge(BinderUtils.GetOptionalVersionAndLanguageCheckForType(this, target.LimitType, this._version)); ParameterExpression parameterExpression = Expression.Parameter(target.LimitType, "target"); ParameterExpression parameterExpression1 = Expression.Parameter(typeof(int), "len"); Expression expression = value.Expression; ParameterExpression parameterExpression2 = Expression.Parameter(expression.Type, "value"); ParameterExpression parameterExpression3 = Expression.Parameter(typeof(int), "index"); ParameterExpression[] parameterExpressionArray = new ParameterExpression[4]; parameterExpressionArray[0] = parameterExpression; parameterExpressionArray[1] = parameterExpression2; parameterExpressionArray[2] = parameterExpression1; parameterExpressionArray[3] = parameterExpression3; Expression[] expressionArray = new Expression[7]; expressionArray[0] = Expression.Assign(parameterExpression, target.Expression.Cast(target.LimitType)); expressionArray[1] = Expression.Assign(parameterExpression2, expression); expressionArray[2] = Expression.Assign(parameterExpression1, Expression.Property(parameterExpression, lengthProperty)); expressionArray[3] = Expression.Assign(parameterExpression3, index.Expression); expressionArray[4] = Expression.IfThen(Expression.LessThan(parameterExpression3, ExpressionCache.Constant(0)), Expression.Assign(parameterExpression3, Expression.Add(parameterExpression3, parameterExpression1))); expressionArray[5] = generateIndexOperation(parameterExpression, parameterExpression3, parameterExpression2); expressionArray[6] = parameterExpression2.Cast(typeof(object)); return(new DynamicMetaObject(Expression.Block(parameterExpressionArray, expressionArray), bindingRestriction)); }
internal static PSMemberInfo ResolveAlias(PSAliasProperty alias, DynamicMetaObject target, HashSet <string> aliases, List <BindingRestrictions> aliasRestrictions) { bool flag; Type type; BindingRestrictions restrictions; if (aliases == null) { aliases = new HashSet <string> { alias.Name }; } else { if (aliases.Contains(alias.Name)) { throw new ExtendedTypeSystemException("CycleInAliasLookup", null, ExtendedTypeSystem.CycleInAlias, new object[] { alias.Name }); } aliases.Add(alias.Name); } PSGetMemberBinder binder = Get(alias.ReferencedMemberName, false); if (binder.HasInstanceMember) { return(null); } PSMemberInfo info = binder.GetPSMemberInfo(target, out restrictions, out flag, out type, aliases, aliasRestrictions); aliasRestrictions.Add(BinderUtils.GetVersionCheck(binder, binder._version)); return(info); }
private DynamicMetaObject CannotIndexTarget(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject value) { BindingRestrictions bindingRestriction = value.PSGetTypeRestriction(); bindingRestriction = bindingRestriction.Merge(BinderUtils.GetVersionCheck(this, this._version)); bindingRestriction = bindingRestriction.Merge(BinderUtils.GetLanguageModeCheckIfHasEverUsedConstrainedLanguage()); Expression[] expressionArray = new Expression[1]; expressionArray[0] = Expression.Constant(target.LimitType); return(target.ThrowRuntimeError(indexes, bindingRestriction, "CannotIndex", ParserStrings.CannotIndex, expressionArray)); }
public override DynamicMetaObject FallbackConvert(DynamicMetaObject target, DynamicMetaObject errorSuggestion) { bool flag; if (!target.HasValue) { return(base.Defer(target, new DynamicMetaObject[0]).WriteToDebugLog(this)); } if (target.Value == AutomationNull.Value) { return(new DynamicMetaObject(Expression.Default(base.Type), target.PSGetTypeRestriction()).WriteToDebugLog(this)); } Type resultType = base.Type; LanguagePrimitives.ConversionData conversion = LanguagePrimitives.FigureConversion(target.Value, resultType, out flag); if ((errorSuggestion != null) && (target.Value is DynamicObject)) { return(errorSuggestion.WriteToDebugLog(this)); } BindingRestrictions restrictions = target.PSGetTypeRestriction().Merge(BinderUtils.GetOptionalVersionAndLanguageCheckForType(this, resultType, this._version)); return(new DynamicMetaObject(InvokeConverter(conversion, target.Expression, resultType, flag, ExpressionCache.InvariantCulture), restrictions).WriteToDebugLog(this)); }
public override DynamicMetaObject FallbackSetMember(DynamicMetaObject target, DynamicMetaObject value, DynamicMetaObject errorSuggestion) { PSMemberInfo info; BindingRestrictions restrictions2; bool flag3; Type type4; if (!target.HasValue || !value.HasValue) { return(base.Defer(target, new DynamicMetaObject[] { value })); } object obj2 = PSObject.Base(target.Value); if (obj2 == null) { return(target.ThrowRuntimeError(new DynamicMetaObject[] { value }, BindingRestrictions.Empty, "PropertyNotFound", ParserStrings.PropertyNotFound, new Expression[] { Expression.Constant(base.Name) }).WriteToDebugLog(this)); } if (value.Value == AutomationNull.Value) { value = new DynamicMetaObject(ExpressionCache.NullConstant, value.PSGetTypeRestriction(), null); } if (this._getMemberBinder.HasInstanceMember && PSGetMemberBinder.TryGetInstanceMember(target.Value, base.Name, out info)) { ParameterExpression expression = Expression.Variable(typeof(PSMemberInfo)); ParameterExpression expression2 = Expression.Variable(typeof(object)); ConditionalExpression expression3 = Expression.Condition(Expression.Call(CachedReflectionInfo.PSGetMemberBinder_TryGetInstanceMember, target.Expression.Cast(typeof(object)), Expression.Constant(base.Name), expression), Expression.Assign(Expression.Property(expression, "Value"), value.Expression.Cast(typeof(object))), base.GetUpdateExpression(typeof(object))); BindingRestrictions restrictions = BinderUtils.GetVersionCheck(this._getMemberBinder, this._getMemberBinder._version).Merge(value.PSGetTypeRestriction()); return(new DynamicMetaObject(Expression.Block(new ParameterExpression[] { expression, expression2 }, new Expression[] { expression3 }), restrictions).WriteToDebugLog(this)); } if (obj2 is IDictionary) { Type genericTypeArg = null; bool flag = PSGetMemberBinder.IsGenericDictionary(obj2, ref genericTypeArg); if (!flag || (genericTypeArg != null)) { bool flag2; Type type = flag ? typeof(IDictionary <,>).MakeGenericType(new Type[] { typeof(string), genericTypeArg }) : typeof(IDictionary); MethodInfo method = type.GetMethod("set_Item"); ParameterExpression left = Expression.Variable(genericTypeArg ?? typeof(object)); Type resultType = left.Type; LanguagePrimitives.ConversionData conversion = LanguagePrimitives.FigureConversion(value.Value, resultType, out flag2); if (conversion.Rank != ConversionRank.None) { Expression right = PSConvertBinder.InvokeConverter(conversion, value.Expression, resultType, flag2, ExpressionCache.InvariantCulture); return(new DynamicMetaObject(Expression.Block(new ParameterExpression[] { left }, new Expression[] { Expression.Assign(left, right), Expression.Call(PSGetMemberBinder.GetTargetExpr(target).Cast(type), method, Expression.Constant(base.Name), right), right.Cast(typeof(object)) }), target.CombineRestrictions(new DynamicMetaObject[] { value })).WriteToDebugLog(this)); } } } info = this._getMemberBinder.GetPSMemberInfo(target, out restrictions2, out flag3, out type4, null, null); restrictions2 = restrictions2.Merge(value.PSGetTypeRestriction()); if (ExecutionContext.HasEverUsedConstrainedLanguage) { restrictions2 = restrictions2.Merge(BinderUtils.GetLanguageModeCheckIfHasEverUsedConstrainedLanguage()); DynamicMetaObject obj3 = PSGetMemberBinder.EnsureAllowedInLanguageMode(LocalPipeline.GetExecutionContextFromTLS().LanguageMode, target, obj2, base.Name, this._static, new DynamicMetaObject[] { value }, restrictions2, "PropertySetterNotSupportedInConstrainedLanguage", ParserStrings.PropertySetConstrainedLanguage); if (obj3 != null) { return(obj3.WriteToDebugLog(this)); } } if (flag3) { if (info == null) { return((errorSuggestion ?? new DynamicMetaObject(Compiler.ThrowRuntimeError("PropertyAssignmentException", ParserStrings.PropertyNotFound, this.ReturnType, new Expression[] { Expression.Constant(base.Name) }), restrictions2)).WriteToDebugLog(this)); } PSPropertyInfo info3 = info as PSPropertyInfo; if (info3 != null) { if (!info3.IsSettable) { Expression innerException = Expression.New(CachedReflectionInfo.SetValueException_ctor, new Expression[] { Expression.Constant("PropertyAssignmentException"), Expression.Constant(null, typeof(Exception)), Expression.Constant(ParserStrings.PropertyIsReadOnly), Expression.NewArrayInit(typeof(object), new Expression[] { Expression.Constant(base.Name) }) }); return(new DynamicMetaObject(Compiler.ThrowRuntimeErrorWithInnerException("PropertyAssignmentException", Expression.Constant(ParserStrings.PropertyIsReadOnly), innerException, this.ReturnType, new Expression[] { Expression.Constant(base.Name) }), restrictions2).WriteToDebugLog(this)); } PSProperty property = info3 as PSProperty; if (property != null) { DotNetAdapter.PropertyCacheEntry adapterData = property.adapterData as DotNetAdapter.PropertyCacheEntry; if (adapterData != null) { Expression expression10; Type propertyType; if (adapterData.member.DeclaringType.IsGenericTypeDefinition) { Expression expression9 = Expression.New(CachedReflectionInfo.SetValueException_ctor, new Expression[] { Expression.Constant("PropertyAssignmentException"), Expression.Constant(null, typeof(Exception)), Expression.Constant(ExtendedTypeSystem.CannotInvokeStaticMethodOnUninstantiatedGenericType), Expression.NewArrayInit(typeof(object), new Expression[] { Expression.Constant(adapterData.member.DeclaringType.FullName) }) }); return(new DynamicMetaObject(Compiler.ThrowRuntimeErrorWithInnerException("PropertyAssignmentException", Expression.Constant(ExtendedTypeSystem.CannotInvokeStaticMethodOnUninstantiatedGenericType), expression9, this.ReturnType, new Expression[] { Expression.Constant(adapterData.member.DeclaringType.FullName) }), restrictions2).WriteToDebugLog(this)); } PropertyInfo member = adapterData.member as PropertyInfo; if (member != null) { propertyType = member.PropertyType; Expression expression11 = this._static ? null : PSGetMemberBinder.GetTargetExpr(target); expression10 = Expression.Property(expression11, member); } else { FieldInfo field = (FieldInfo)adapterData.member; propertyType = field.FieldType; Expression expression12 = this._static ? null : PSGetMemberBinder.GetTargetExpr(target); expression10 = Expression.Field(expression12, field); } Type underlyingType = Nullable.GetUnderlyingType(propertyType); Type type7 = underlyingType ?? propertyType; ParameterExpression expression13 = Expression.Variable(type7); Expression expression14 = (underlyingType != null) ? ((value.Value == null) ? ((Expression)Expression.Constant(null, propertyType)) : ((Expression)Expression.New(propertyType.GetConstructor(new Type[] { underlyingType }), new Expression[] { expression13 }))) : ((Expression)expression13); Expression expression15 = (type7.Equals(typeof(object)) && value.LimitType.Equals(typeof(PSObject))) ? Expression.Call(CachedReflectionInfo.PSObject_Base, value.Expression.Cast(typeof(PSObject))) : value.CastOrConvert(type7); Expression expr = Expression.Block(new ParameterExpression[] { expression13 }, new Expression[] { Expression.Assign(expression13, expression15), Expression.Assign(expression10, expression14), expression13.Cast(typeof(object)) }); ParameterExpression variable = Expression.Variable(typeof(Exception)); return(new DynamicMetaObject(Expression.TryCatch(expr.Cast(typeof(object)), new CatchBlock[] { Expression.Catch(variable, Expression.Block(Expression.Call(CachedReflectionInfo.ExceptionHandlingOps_ConvertToMethodInvocationException, variable, Expression.Constant(typeof(SetValueInvocationException)), Expression.Constant(base.Name), ExpressionCache.Constant(0), Expression.Constant(null, typeof(MemberInfo))), Expression.Rethrow(typeof(object)))) }), restrictions2).WriteToDebugLog(this)); } } PSCodeProperty property2 = info3 as PSCodeProperty; if (property2 != null) { ParameterExpression expression17 = Expression.Variable(typeof(object)); return(new DynamicMetaObject(Expression.Block(new ParameterExpression[] { expression17 }, new Expression[] { Expression.Assign(expression17, value.CastOrConvert(expression17.Type)), PSInvokeMemberBinder.InvokeMethod(property2.SetterCodeReference, null, new DynamicMetaObject[] { target, value }, false), expression17 }), restrictions2).WriteToDebugLog(this)); } PSScriptProperty property3 = info3 as PSScriptProperty; if (property3 != null) { return(new DynamicMetaObject(Expression.Call(Expression.Constant(property3), CachedReflectionInfo.PSScriptProperty_InvokeSetter, PSGetMemberBinder.GetTargetExpr(target), value.Expression.Cast(typeof(object))), restrictions2).WriteToDebugLog(this)); } } if (errorSuggestion != null) { return(errorSuggestion.WriteToDebugLog(this)); } } return(new DynamicMetaObject(Expression.Call(CachedReflectionInfo.PSSetMemberBinder_SetAdaptedValue, PSGetMemberBinder.GetTargetExpr(target).Cast(typeof(object)), Expression.Constant(base.Name), value.Expression.Cast(typeof(object))), restrictions2).WriteToDebugLog(this)); }
private DynamicMetaObject InvokeIndexer(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject value, DynamicMetaObject errorSuggestion, string methodName) { DynamicMetaObject dynamicMetaObject; Func <Expression, Expression, Expression, Expression> func = null; MethodInfo methodInfo = PSInvokeMemberBinder.FindBestMethod(target, indexes.Append <DynamicMetaObject>(value), string.Concat("set_", methodName), false, this._constraints); if (methodInfo != null) { ParameterInfo[] parameters = methodInfo.GetParameters(); if ((int)parameters.Length == (int)indexes.Length + 1) { Expression[] expressionArray = new Expression[(int)parameters.Length]; int num = 0; while (num < (int)parameters.Length) { Type parameterType = parameters[num].ParameterType; Expression[] expressionArray1 = expressionArray; int num1 = num; if (num == (int)parameters.Length - 1) { dynamicMetaObject = value; } else { dynamicMetaObject = indexes[num]; } expressionArray1[num1] = PSGetIndexBinder.ConvertIndex(dynamicMetaObject, parameterType); if (expressionArray[num] != null) { num++; } else { DynamicMetaObject dynamicMetaObject1 = errorSuggestion; DynamicMetaObject dynamicMetaObject2 = dynamicMetaObject1; if (dynamicMetaObject1 == null) { dynamicMetaObject2 = PSConvertBinder.ThrowNoConversion(target, parameterType, this, this._version, indexes.Append <DynamicMetaObject>(value).ToArray <DynamicMetaObject>()); } return(dynamicMetaObject2); } } if ((int)parameters.Length == 2 && parameters[0].ParameterType.Equals(typeof(int)) && target.Value as IDictionary == null) { PropertyInfo property = target.LimitType.GetProperty("Length"); PropertyInfo propertyInfo = property; if (property == null) { propertyInfo = target.LimitType.GetProperty("Count"); } PropertyInfo propertyInfo1 = propertyInfo; if (propertyInfo1 != null) { PSSetIndexBinder pSSetIndexBinder = this; DynamicMetaObject dynamicMetaObject3 = new DynamicMetaObject(target.Expression.Cast(target.LimitType), target.PSGetTypeRestriction()); DynamicMetaObject dynamicMetaObject4 = new DynamicMetaObject(expressionArray[0], indexes[0].PSGetTypeRestriction()); DynamicMetaObject dynamicMetaObject5 = new DynamicMetaObject(expressionArray[1], value.PSGetTypeRestriction()); PropertyInfo propertyInfo2 = propertyInfo1; if (func == null) { func = (Expression t, Expression i, Expression v) => Expression.Call(t, methodInfo, i, v); } return(pSSetIndexBinder.IndexWithNegativeChecks(dynamicMetaObject3, dynamicMetaObject4, dynamicMetaObject5, propertyInfo2, func)); } } BindingRestrictions bindingRestriction = target.CombineRestrictions(indexes).Merge(value.PSGetTypeRestriction()); bindingRestriction = bindingRestriction.Merge(BinderUtils.GetVersionCheck(this, this._version)); bindingRestriction = bindingRestriction.Merge(BinderUtils.GetLanguageModeCheckIfHasEverUsedConstrainedLanguage()); Expression expression = expressionArray[(int)expressionArray.Length - 1]; ParameterExpression parameterExpression = Expression.Parameter(expression.Type, "value"); expressionArray[(int)expressionArray.Length - 1] = parameterExpression; ParameterExpression[] parameterExpressionArray = new ParameterExpression[1]; parameterExpressionArray[0] = parameterExpression; Expression[] expressionArray2 = new Expression[3]; expressionArray2[0] = Expression.Assign(parameterExpression, expression); expressionArray2[1] = Expression.Call(target.Expression.Cast(target.LimitType), methodInfo, expressionArray); expressionArray2[2] = parameterExpression.Cast(typeof(object)); return(new DynamicMetaObject(Expression.Block(parameterExpressionArray, expressionArray2), bindingRestriction)); } else { DynamicMetaObject dynamicMetaObject6 = errorSuggestion; DynamicMetaObject dynamicMetaObject7 = dynamicMetaObject6; if (dynamicMetaObject6 == null) { dynamicMetaObject7 = this.CannotIndexTarget(target, indexes, value); } return(dynamicMetaObject7); } } else { DynamicMetaObject dynamicMetaObject8 = errorSuggestion; DynamicMetaObject dynamicMetaObject9 = dynamicMetaObject8; if (dynamicMetaObject8 == null) { dynamicMetaObject9 = this.CannotIndexTarget(target, indexes, value); } return(dynamicMetaObject9); } }
public override DynamicMetaObject FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion) { PSMemberInfo pSMemberInfo = null; BindingRestrictions bindingRestriction = null; bool flag = false; Type type = null; Expression expression; Type value; MethodInformation[] methodInformationArray; Type type1; if (target.HasValue) { DynamicMetaObject[] dynamicMetaObjectArray = args; if (!dynamicMetaObjectArray.Where <DynamicMetaObject>((DynamicMetaObject arg) => !arg.HasValue).Any <DynamicMetaObject>()) { object obj = PSObject.Base(target.Value); if (obj != null) { if (!this._getMemberBinder.HasInstanceMember || !PSGetMemberBinder.TryGetInstanceMember(target.Value, base.Name, out pSMemberInfo)) { PSMethodInfo pSMethodInfo = this._getMemberBinder.GetPSMemberInfo(target, out bindingRestriction, out flag, out type, null, null) as PSMethodInfo; DynamicMetaObject[] dynamicMetaObjectArray1 = args; BindingRestrictions bindingRestriction1 = bindingRestriction; bindingRestriction = dynamicMetaObjectArray1.Aggregate <DynamicMetaObject, BindingRestrictions>(bindingRestriction1, (BindingRestrictions current, DynamicMetaObject arg) => current.Merge(arg.PSGetMethodArgumentRestriction())); if (ExecutionContext.HasEverUsedConstrainedLanguage) { bindingRestriction = bindingRestriction.Merge(BinderUtils.GetLanguageModeCheckIfHasEverUsedConstrainedLanguage()); ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS(); DynamicMetaObject dynamicMetaObject = PSGetMemberBinder.EnsureAllowedInLanguageMode(executionContextFromTLS.LanguageMode, target, obj, base.Name, this._static, args, bindingRestriction, "MethodInvocationNotSupportedInConstrainedLanguage", ParserStrings.InvokeMethodConstrainedLanguage); if (dynamicMetaObject != null) { return(dynamicMetaObject.WriteToDebugLog(this)); } } if (flag) { PSObject pSObject = target.Value as PSObject; if (pSObject != null && (obj.GetType().Equals(typeof(Hashtable)) || obj.GetType().Equals(typeof(ArrayList)))) { if (!pSObject.isDeserialized) { bindingRestriction = bindingRestriction.Merge(BindingRestrictions.GetExpressionRestriction(Expression.Not(Expression.Field(target.Expression.Cast(typeof(PSObject)), CachedReflectionInfo.PSObject_isDeserialized)))); } else { pSMethodInfo = null; bindingRestriction = bindingRestriction.Merge(BindingRestrictions.GetExpressionRestriction(Expression.Field(target.Expression.Cast(typeof(PSObject)), CachedReflectionInfo.PSObject_isDeserialized))); } } PSMethod pSMethod = pSMethodInfo as PSMethod; if (pSMethod == null) { PSScriptMethod pSScriptMethod = pSMethodInfo as PSScriptMethod; if (pSScriptMethod == null) { PSCodeMethod pSCodeMethod = pSMethodInfo as PSCodeMethod; if (pSCodeMethod == null) { PSParameterizedProperty pSParameterizedProperty = pSMethodInfo as PSParameterizedProperty; if (pSParameterizedProperty == null) { if (errorSuggestion == null) { if (this._static) { value = (Type)target.Value; } else { value = PSObject.Base(target.Value).GetType(); } Type type2 = value; if (!this._static && !this._nonEnumerating && target.Value != AutomationNull.Value) { DynamicMetaObject dynamicMetaObject1 = PSEnumerableBinder.IsEnumerable(target); if (dynamicMetaObject1 != null) { return(this.InvokeMemberOnCollection(dynamicMetaObject1, args, type2, bindingRestriction)); } } Expression[] expressionArray = new Expression[2]; expressionArray[0] = Expression.Constant(type2.FullName); expressionArray[1] = Expression.Constant(base.Name); return(new DynamicMetaObject(Compiler.ThrowRuntimeError("MethodNotFound", ParserStrings.MethodNotFound, expressionArray), bindingRestriction).WriteToDebugLog(this)); } else { return(errorSuggestion.WriteToDebugLog(this)); } } else { DotNetAdapter.ParameterizedPropertyCacheEntry parameterizedPropertyCacheEntry = (DotNetAdapter.ParameterizedPropertyCacheEntry)pSParameterizedProperty.adapterData; PSInvokeMemberBinder pSInvokeMemberBinder = this; DynamicMetaObject dynamicMetaObject2 = target; DynamicMetaObject[] dynamicMetaObjectArray2 = args; BindingRestrictions bindingRestriction2 = bindingRestriction; if (this._propertySetter) { methodInformationArray = parameterizedPropertyCacheEntry.setterInformation; } else { methodInformationArray = parameterizedPropertyCacheEntry.getterInformation; } if (this._propertySetter) { type1 = typeof(SetValueInvocationException); } else { type1 = typeof(GetValueInvocationException); } return(pSInvokeMemberBinder.InvokeDotNetMethod(dynamicMetaObject2, dynamicMetaObjectArray2, bindingRestriction2, methodInformationArray, type1).WriteToDebugLog(this)); } } else { return(new DynamicMetaObject(PSInvokeMemberBinder.InvokeMethod(pSCodeMethod.CodeReference, null, args.Prepend <DynamicMetaObject>(target).ToArray <DynamicMetaObject>(), false).Cast(typeof(object)), bindingRestriction).WriteToDebugLog(this)); } } else { MethodInfo pSScriptMethodInvokeScript = CachedReflectionInfo.PSScriptMethod_InvokeScript; ConstantExpression constantExpression = Expression.Constant(base.Name); ConstantExpression constantExpression1 = Expression.Constant(pSScriptMethod.Script); Expression expression1 = target.Expression.Cast(typeof(object)); Type type3 = typeof(object); DynamicMetaObject[] dynamicMetaObjectArray3 = args; return(new DynamicMetaObject(Expression.Call(pSScriptMethodInvokeScript, constantExpression, constantExpression1, expression1, Expression.NewArrayInit(type3, dynamicMetaObjectArray3.Select <DynamicMetaObject, Expression>((DynamicMetaObject e) => e.Expression.Cast(typeof(object))))), bindingRestriction).WriteToDebugLog(this)); } } else { DotNetAdapter.MethodCacheEntry methodCacheEntry = (DotNetAdapter.MethodCacheEntry)pSMethod.adapterData; return(this.InvokeDotNetMethod(target, args, bindingRestriction, methodCacheEntry.methodInformationStructures, typeof(MethodException)).WriteToDebugLog(this)); } } else { if (!this._propertySetter) { MethodInfo pSInvokeMemberBinderInvokeAdaptedMember = CachedReflectionInfo.PSInvokeMemberBinder_InvokeAdaptedMember; Expression expression2 = PSGetMemberBinder.GetTargetExpr(target).Cast(typeof(object)); ConstantExpression constantExpression2 = Expression.Constant(base.Name); Type type4 = typeof(object); DynamicMetaObject[] dynamicMetaObjectArray4 = args; expression = Expression.Call(pSInvokeMemberBinderInvokeAdaptedMember, expression2, constantExpression2, Expression.NewArrayInit(type4, dynamicMetaObjectArray4.Select <DynamicMetaObject, Expression>((DynamicMetaObject arg) => arg.Expression.Cast(typeof(object))))); } else { MethodInfo pSInvokeMemberBinderInvokeAdaptedSetMember = CachedReflectionInfo.PSInvokeMemberBinder_InvokeAdaptedSetMember; Expression expression3 = PSGetMemberBinder.GetTargetExpr(target).Cast(typeof(object)); ConstantExpression constantExpression3 = Expression.Constant(base.Name); Type type5 = typeof(object); IEnumerable <DynamicMetaObject> dynamicMetaObjects = args.Take <DynamicMetaObject>((int)args.Length - 1); expression = Expression.Call(pSInvokeMemberBinderInvokeAdaptedSetMember, expression3, constantExpression3, Expression.NewArrayInit(type5, dynamicMetaObjects.Select <DynamicMetaObject, Expression>((DynamicMetaObject arg) => arg.Expression.Cast(typeof(object)))), args.Last <DynamicMetaObject>().Expression.Cast(typeof(object))); } return(new DynamicMetaObject(expression, bindingRestriction).WriteToDebugLog(this)); } } else { ParameterExpression parameterExpression = Expression.Variable(typeof(PSMethodInfo)); MethodCallExpression methodCallExpression = Expression.Call(CachedReflectionInfo.PSInvokeMemberBinder_TryGetInstanceMethod, target.Expression.Cast(typeof(object)), Expression.Constant(base.Name), parameterExpression); ParameterExpression parameterExpression1 = parameterExpression; MethodInfo pSMethodInfoInvoke = CachedReflectionInfo.PSMethodInfo_Invoke; Expression[] expressionArray1 = new Expression[1]; Expression[] expressionArray2 = expressionArray1; int num = 0; Type type6 = typeof(object); DynamicMetaObject[] dynamicMetaObjectArray5 = args; expressionArray2[num] = Expression.NewArrayInit(type6, dynamicMetaObjectArray5.Select <DynamicMetaObject, Expression>((DynamicMetaObject dmo) => dmo.Expression.Cast(typeof(object)))); ConditionalExpression conditionalExpression = Expression.Condition(methodCallExpression, Expression.Call(parameterExpression1, pSMethodInfoInvoke, expressionArray1), base.GetUpdateExpression(typeof(object))); ParameterExpression[] parameterExpressionArray = new ParameterExpression[1]; parameterExpressionArray[0] = parameterExpression; Expression[] expressionArray3 = new Expression[1]; expressionArray3[0] = conditionalExpression; return(new DynamicMetaObject(Expression.Block(parameterExpressionArray, expressionArray3), BinderUtils.GetVersionCheck(this._getMemberBinder, this._getMemberBinder._version)).WriteToDebugLog(this)); } } else { return(target.ThrowRuntimeError(args, BindingRestrictions.Empty, "InvokeMethodOnNull", ParserStrings.InvokeMethodOnNull, new Expression[0]).WriteToDebugLog(this)); } } } return(base.Defer(args.Prepend <DynamicMetaObject>(target).ToArray <DynamicMetaObject>())); }
internal static DynamicMetaObject ThrowNoConversion(DynamicMetaObject target, Type toType, DynamicMetaObjectBinder binder, int currentVersion, params DynamicMetaObject[] args) { Expression expression = Expression.Call(CachedReflectionInfo.LanguagePrimitives_ThrowInvalidCastException, target.Expression.Cast(typeof(object)), Expression.Constant(toType)); if (!binder.ReturnType.Equals(typeof(void))) { expression = Expression.Block(expression, Expression.Default(binder.ReturnType)); } return(new DynamicMetaObject(expression, target.CombineRestrictions(args).Merge(BinderUtils.GetOptionalVersionAndLanguageCheckForType(binder, toType, currentVersion)))); }
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)); }
internal PSMemberInfo GetPSMemberInfo(DynamicMetaObject target, out BindingRestrictions restrictions, out bool canOptimize, out Type aliasConversionType, HashSet <string> aliases = null, List <BindingRestrictions> aliasRestrictions = null) { lock (this) { aliasConversionType = null; if (this._static) { restrictions = typeof(Type).IsAssignableFrom(target.LimitType) ? BindingRestrictions.GetInstanceRestriction(target.Expression, target.Value) : target.PSGetTypeRestriction(); restrictions = restrictions.Merge(BinderUtils.GetVersionCheck(this, this._version)); canOptimize = true; return(PSObject.GetStaticCLRMember(target.Value, base.Name)); } canOptimize = false; PSMemberInfo info = null; ConsolidatedString types = null; ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS(); TypeTable typeTable = (executionContextFromTLS != null) ? executionContextFromTLS.TypeTable : null; if (this._hasTypeTableMember) { types = PSObject.GetTypeNames(target.Value); if (typeTable != null) { info = typeTable.GetMembers <PSMemberInfo>(types)[base.Name]; if (info != null) { canOptimize = true; } } } object obj2 = PSObject.Base(target.Value); PSObject.AdapterSet mappedAdapter = PSObject.GetMappedAdapter(obj2, typeTable); if (info == null) { canOptimize = mappedAdapter.OriginalAdapter.SiteBinderCanOptimize; if (canOptimize) { info = mappedAdapter.OriginalAdapter.BaseGetMember <PSMemberInfo>(obj2, base.Name); } } if (((info == null) && canOptimize) && (mappedAdapter.DotNetAdapter != null)) { info = mappedAdapter.DotNetAdapter.BaseGetMember <PSMemberInfo>(obj2, base.Name); } restrictions = BinderUtils.GetVersionCheck(this, this._version); PSAliasProperty alias = info as PSAliasProperty; if (alias != null) { aliasConversionType = alias.ConversionType; if (aliasRestrictions == null) { aliasRestrictions = new List <BindingRestrictions>(); } info = ResolveAlias(alias, target, aliases, aliasRestrictions); if (info == null) { canOptimize = false; } else { foreach (BindingRestrictions restrictions2 in aliasRestrictions) { restrictions = restrictions.Merge(restrictions2); } } } if (this._hasInstanceMember) { restrictions = restrictions.Merge(this.NotInstanceMember(target)); } restrictions = restrictions.Merge(target.PSGetTypeRestriction()); if (this._hasTypeTableMember) { restrictions = restrictions.Merge(BindingRestrictions.GetInstanceRestriction(Expression.Call(CachedReflectionInfo.PSGetMemberBinder_GetTypeTableFromTLS, new Expression[0]), typeTable)); restrictions = restrictions.Merge(BindingRestrictions.GetExpressionRestriction(Expression.Call(CachedReflectionInfo.PSGetMemberBinder_IsTypeNameSame, target.Expression.Cast(typeof(object)), Expression.Constant(types.Key)))); } return(info); } }
private DynamicMetaObject CannotIndexTarget(DynamicMetaObject target, DynamicMetaObject[] indexes) { return(new DynamicMetaObject(Expression.Call(CachedReflectionInfo.ArrayOps_GetNonIndexable, target.Expression.Cast(typeof(object)), Expression.NewArrayInit(typeof(object), (IEnumerable <Expression>)(from d in indexes select d.Expression.Cast(typeof(object))))), target.CombineRestrictions(indexes).Merge(BinderUtils.GetVersionCheck(this, this._version)).Merge(BinderUtils.GetLanguageModeCheckIfHasEverUsedConstrainedLanguage()))); }
private DynamicMetaObject InvokeIndexer(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject errorSuggestion, string methodName) { Func <Expression, Expression, Expression> generateIndexOperation = null; MethodInfo getter = PSInvokeMemberBinder.FindBestMethod(target, indexes, "get_" + methodName, false, this._constraints); if (getter == null) { return(this.CheckForSlicing(target, indexes) ?? (errorSuggestion ?? this.CannotIndexTarget(target, indexes))); } ParameterInfo[] parameters = getter.GetParameters(); if (parameters.Length != indexes.Length) { if ((parameters.Length == 1) && this._allowSlicing) { return(this.InvokeSlicingIndexer(target, indexes)); } return(errorSuggestion ?? this.CannotIndexTarget(target, indexes)); } if (parameters.Length == 1) { DynamicMetaObject obj2 = this.CheckForSlicing(target, indexes); if (obj2 != null) { return(obj2); } } Expression[] arguments = new Expression[parameters.Length]; for (int j = 0; j < parameters.Length; j++) { Type parameterType = parameters[j].ParameterType; arguments[j] = ConvertIndex(indexes[j], parameterType); if (arguments[j] == null) { return(errorSuggestion ?? PSConvertBinder.ThrowNoConversion(target, parameterType, this, this._version, indexes)); } } if (((parameters.Length == 1) && parameters[0].ParameterType.Equals(typeof(int))) && CanIndexFromEndWithNegativeIndex(target)) { PropertyInfo lengthProperty = target.LimitType.GetProperty("Count") ?? target.LimitType.GetProperty("Length"); if (lengthProperty != null) { if (generateIndexOperation == null) { generateIndexOperation = (t, i) => Expression.Call(t, getter, new Expression[] { i }).Cast(typeof(object)); } return(this.IndexWithNegativeChecks(new DynamicMetaObject(target.Expression.Cast(target.LimitType), target.PSGetTypeRestriction()), new DynamicMetaObject(arguments[0], indexes[0].PSGetTypeRestriction()), lengthProperty, generateIndexOperation)); } } return(new DynamicMetaObject(this.SafeIndexResult(Expression.Call(target.Expression.Cast(target.LimitType), getter, arguments)), target.CombineRestrictions(indexes).Merge(BinderUtils.GetVersionCheck(this, this._version)).Merge(BinderUtils.GetLanguageModeCheckIfHasEverUsedConstrainedLanguage()))); }
private DynamicMetaObject GetIndexDictionary(DynamicMetaObject target, DynamicMetaObject[] indexes, Type idictionary) { bool flag; if (indexes.Length > 1) { return(null); } MethodInfo method = idictionary.GetMethod("TryGetValue"); ParameterInfo[] parameters = method.GetParameters(); Type parameterType = parameters[0].ParameterType; LanguagePrimitives.ConversionData conversion = LanguagePrimitives.FigureConversion(indexes[0].Value, parameterType, out flag); if (conversion.Rank == ConversionRank.None) { return(null); } if (indexes[0].LimitType.IsArray && !parameterType.IsArray) { return(null); } BindingRestrictions restrictions = target.CombineRestrictions(indexes).Merge(BinderUtils.GetOptionalVersionAndLanguageCheckForType(this, parameterType, this._version)); Expression expression = PSConvertBinder.InvokeConverter(conversion, indexes[0].Expression, parameterType, flag, ExpressionCache.InvariantCulture); ParameterExpression expression2 = Expression.Parameter(parameters[1].ParameterType.GetElementType(), "outParam"); return(new DynamicMetaObject(Expression.Block(new ParameterExpression[] { expression2 }, new Expression[] { Expression.Condition(Expression.Call(target.Expression.Cast(idictionary), method, expression, expression2), expression2.Cast(typeof(object)), this.GetNullResult()) }), restrictions)); }