System.Linq.Expressions.Expression.Dynamic C# (CSharp) Method

Dynamic() public static method

Creates a DynamicExpression that represents a dynamic operation bound by the provided CallSiteBinder.
The DelegateType property of the result will be inferred from the types of the arguments and the specified return type.
public static Dynamic ( CallSiteBinder binder, Type returnType, Expression arg0, Expression arg1, Expression arg2 ) : DynamicExpression
binder CallSiteBinder The runtime binder for the dynamic operation.
returnType Type The result type of the dynamic expression.
arg0 Expression The first argument to the dynamic operation.
arg1 Expression The second argument to the dynamic operation.
arg2 Expression The third argument to the dynamic operation.
return DynamicExpression
        public static DynamicExpression Dynamic(CallSiteBinder binder, Type returnType, Expression arg0, Expression arg1, Expression arg2)
        {
            ContractUtils.RequiresNotNull(binder, nameof(binder));
            ValidateDynamicArgument(arg0, nameof(arg0));
            ValidateDynamicArgument(arg1, nameof(arg1));
            ValidateDynamicArgument(arg2, nameof(arg2));

            DelegateHelpers.TypeInfo info = DelegateHelpers.GetNextTypeInfo(
                returnType,
                DelegateHelpers.GetNextTypeInfo(
                    arg2.Type,
                    DelegateHelpers.GetNextTypeInfo(
                        arg1.Type,
                        DelegateHelpers.GetNextTypeInfo(
                            arg0.Type,
                            DelegateHelpers.NextTypeInfo(typeof(CallSite))
                        )
                    )
                )
            );

            Type delegateType = info.DelegateType;
            if (delegateType == null)
            {
                delegateType = info.MakeDelegateType(returnType, arg0, arg1, arg2);
            }

            return DynamicExpression.Make(returnType, delegateType, binder, arg0, arg1, arg2);
        }

Same methods

Expression::Dynamic ( CallSiteBinder binder, Type returnType ) : DynamicExpression
Expression::Dynamic ( CallSiteBinder binder, Type returnType, Expression arg0 ) : DynamicExpression
Expression::Dynamic ( CallSiteBinder binder, Type returnType, Expression arg0, Expression arg1 ) : DynamicExpression
Expression::Dynamic ( CallSiteBinder binder, Type returnType, Expression arg0, Expression arg1, Expression arg2, Expression arg3 ) : DynamicExpression
Expression::Dynamic ( CallSiteBinder binder, Type returnType, IEnumerable arguments ) : DynamicExpression

Usage Example

コード例 #1
0
        private object CastResult(object result, Type targetType)
        {
            if (result == null || result == DependencyProperty.UnsetValue || result == System.Windows.Data.Binding.DoNothing || targetType == null || targetType == typeof(object))
            {
                return(result);
            }

            if (targetType == typeof(string))
            {
                return(result.ToString());
            }

            Func <object, object> cast;

            if (!castFunctions.TryGetValue(targetType, out cast))
            {
                ParameterExpression par = Expression.Parameter(typeof(object));
                cast = Expression.Lambda <Func <object, object> >(Expression.Convert(Expression.Dynamic(Binder.Convert(CSharpBinderFlags.ConvertExplicit, targetType, typeof(object)), targetType, par), typeof(object)), par).Compile();
                castFunctions.TryAdd(targetType, cast);
            }
            if (cast != null)
            {
                try
                {
                    result = cast(result);
                }
                catch
                {
                    castFunctions[targetType] = null;
                }
            }
            return(result);
        }
All Usage Examples Of System.Linq.Expressions.Expression::Dynamic
Expression