Bind() публичный статический Метод

Creates a MemberAssignment binding the specified value to the given member.
public static Bind ( MemberInfo member, Expression expression ) : MemberAssignment
member System.Reflection.MemberInfo The for the member which is being assigned to.
expression Expression The value to be assigned to .
Результат MemberAssignment
        public void MemberInit_bind()
        {
            var expression =
                LinqExpression.MemberInit(
                    LinqExpression.New(
                        typeof(SampleClass)),
                    LinqExpression.Bind(
                        typeof(SampleClass).GetField(nameof(SampleClass.InstanceField)),
                        LinqExpression.Constant(string.Empty)));

            ShouldRoundrip(expression);
        }
Пример #2
0
            public Expression CreateInitExpression()
            {
                List <MemberBinding> bindings = new List <MemberBinding>();

                var modelType    = ((PropertyInfo)ModelProperty.Member).PropertyType;
                var responseType = ResponseProperty.PropertyType;
                var props        = responseType.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(x => x.CanWrite);

                foreach (var p in props)
                {
                    IncludeTree include;
                    Expression  bind = null;

                    if (!Includes.TryGetValue(p.Name, out include))
                    {
                        NavigationPropertyAttribute navAttr = p.GetCustomAttribute <NavigationPropertyAttribute>();

                        if (navAttr != null)
                        {
                            var navModelProp = Expression.Property(ModelProperty, modelType.GetProperty(navAttr.NavigationProperty));
                            var navProp      = Expression.Property(navModelProp, ((PropertyInfo)navModelProp.Member).PropertyType.GetProperty(navAttr.Property));
                            bind = navProp;
                        }
                        else if (IsMappeable(p))
                        {
                            bind = Expression.Property(ModelProperty, modelType.GetProperty(p.Name));
                        }
                    }
                    else
                    {
                        bind = include.CreateInitExpression();
                    }

                    if (bind != null)
                    {
                        bindings.Add(Expression.Bind(p, bind));
                    }
                }

                NewExpression        newResponse    = Expression.New(responseType);
                MemberInitExpression initExpression = Expression.MemberInit(newResponse, bindings);

                return(initExpression);
            }
Пример #3
0
        private static Expression CreateValueExpression(DataType value, Expression result)
        {
            Expression FromString(string str)
            {
                return(result is MemberExpression me
                    ? (Expression)Expression.MemberInit(
                           Expression.New(me.Type.GetConstructor(Array.Empty <Type>())),
                           Expression.Bind(
                               me.Type.GetProperty("ObjectValue"),
                               Expression.Constant(str)))
                    : Expression.Constant(value));
            }

            return(value switch
            {
                Code code => FromString(code.Value),
                FhirString str => FromString(str.Value),
                _ => Expression.Constant(value)
            });
Пример #4
0
        public static System.Linq.Expressions.Expression <Func <object[], object> > CreateComplexExpression(string p = null)
        {
            var stateParamExpr = SysExpr.Parameter(typeof(object[]), p);

            var expr = SysExpr.Lambda <Func <object[], object> >(
                SysExpr.MemberInit(
                    SysExpr.New(_ctorOfA,
                                SysExpr.New(_ctorOfB),
                                SysExpr.Convert(SysExpr.ArrayIndex(stateParamExpr, SysExpr.Constant(11)), typeof(string)),
                                SysExpr.NewArrayInit(typeof(ID),
                                                     SysExpr.New(_ctorOfD1),
                                                     SysExpr.New(_ctorOfD2))),
                    SysExpr.Bind(_propAProp,
                                 SysExpr.New(_ctorOfP,
                                             SysExpr.New(_ctorOfB))),
                    SysExpr.Bind(_fieldABop,
                                 SysExpr.New(_ctorOfB))),
                stateParamExpr);

            return(expr);
        }
Пример #5
0
        private static Func <TIn, TOut> GetFunc()
        {
            ParameterExpression  parameterExpression = Expression.Parameter(typeof(TIn), "p");
            List <MemberBinding> memberBindingList   = new List <MemberBinding>();

            foreach (var item in typeof(TOut).GetProperties())
            {
                if (!item.CanWrite)
                {
                    continue;
                }

                MemberExpression property      = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
                MemberBinding    memberBinding = Expression.Bind(item, property);
                memberBindingList.Add(memberBinding);
            }

            MemberInitExpression           memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());
            Expression <Func <TIn, TOut> > lambda = Expression.Lambda <Func <TIn, TOut> >(memberInitExpression, new ParameterExpression[] { parameterExpression });

            return(lambda.Compile());
        }
Пример #6
0
        public Expression <Func <T, TResponse> > Select <TResponse>(string include = null)
        {
            List <MemberBinding> bindings = new List <MemberBinding>();

            foreach (var p in ResponseType.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(x => x.CanWrite))
            {
                NavigationPropertyAttribute navAttr = p.GetCustomAttribute <NavigationPropertyAttribute>();
                if (navAttr != null)
                {
                    bindings.Add(Expression.Bind(p, Expression.Property(Expression.Property(Parameter, navAttr.NavigationProperty), navAttr.Property)));
                }
                else if (IncludeTree.IsMappeable(p))
                {
                    bindings.Add(Expression.Bind(p, Expression.Property(Parameter, p.Name)));
                }
            }

            if (!string.IsNullOrWhiteSpace(include))
            {
                var incTrees = IncludeTree.Build(include, ResponseType, ModelType, Parameter);
                foreach (var item in incTrees)
                {
                    bindings.Add(Expression.Bind(item.ResponseProperty, item.CreateInitExpression()));
                }
            }

            MemberInitExpression initExpression = Expression.MemberInit(Expression.New(typeof(TResponse)), bindings);

            Expression current = Parameter;

            while (current != null && !(current is ParameterExpression))
            {
                current = ((MemberExpression)current).Expression;
            }

            return(Expression.Lambda <Func <T, TResponse> >(initExpression, (ParameterExpression)current));
        }
Пример #7
0
 public override System.Linq.Expressions.MemberBinding ToMemberBinding() =>
 SysExpr.Bind(Member, Expression.ToExpression());
Пример #8
0
        private MemberAssignment VisitMemberAssignment(MemberAssignment assignment)
        {
            var e = Visit(assignment.Expression);

            return(e != assignment.Expression ? Expression.Bind(assignment.Member, e) : assignment);
        }
 /// <summary>
 /// Constructs an assigment expression, => Expression.Bind(member, expression)
 /// </summary>
 /// <param name="memberInfo">The member info.</param>
 /// <param name="expression">The right part.</param>
 /// <returns>The assignment expression.</returns>
 public static MemberAssignment AssignTo(this MemberInfo memberInfo, Expression expression) =>
 Expression.Bind(memberInfo, expression);