public static void CreateMemberInitExpression() { System.Linq.Expressions.NewExpression newAnimal = System.Linq.Expressions.Expression.New(typeof(Animal)); System.Reflection.MemberInfo speciesMember = typeof(Animal).GetMember("Species")[0]; System.Reflection.MemberInfo ageMember = typeof(Animal).GetMember("Age")[0]; // Create a MemberBinding object for each member // that you want to initialize. System.Linq.Expressions.MemberBinding speciesMemberBinding = System.Linq.Expressions.Expression.Bind( speciesMember, System.Linq.Expressions.Expression.Constant("horse")); System.Linq.Expressions.MemberBinding ageMemberBinding = System.Linq.Expressions.Expression.Bind( ageMember, System.Linq.Expressions.Expression.Constant(12)); // Create a MemberInitExpression that represents initializing // two members of the 'Animal' class. System.Linq.Expressions.MemberInitExpression memberInitExpression = System.Linq.Expressions.Expression.MemberInit( newAnimal, speciesMemberBinding, ageMemberBinding); Console.WriteLine(memberInitExpression.ToString()); // This code produces the following output: // // new Animal() {Species = "horse", Age = 12} }
protected override System.Linq.Expressions.MemberBinding VisitBinding(System.Linq.Expressions.MemberBinding binding) { if ((this.ExcuteType == DMSExcuteType.UPDATE || this.ExcuteType == DMSExcuteType.INSERT || this.ExcuteType == DMSExcuteType.INSERTIDENTITY || this.ExcuteType == DMSExcuteType.INSERT_SELECT || this.ExcuteType == DMSExcuteType.UPDATE_WHERE) && binding.BindingType == System.Linq.Expressions.MemberBindingType.Assignment) { string text = string.Empty; MemberInfo memberInfo = binding.Member; this._CurrentMemberInfo = memberInfo; if (this.ExcuteType == DMSExcuteType.UPDATE_WHERE) { var lastTableKey = this.TableKeys.Where(q => q.AssemblyQualifiedName == memberInfo.DeclaringType.AssemblyQualifiedName).LastOrDefault(); if (lastTableKey != null) { text = lastTableKey.TableSpecialName; if (this.SplitExpression.TableMapping.TokenFlag == true) { text = this.Provider.BuildColumnName(text); } this._strSql.Append(text); this._strSql.Append(this.Provider.TableToken); } } text = GetMemberInfoName(memberInfo.Name); if (this.SplitExpression.TableMapping.TokenFlag == true) { text = this.Provider.BuildColumnName(text); } this._strSql.Append(text); if (this.ExcuteType == DMSExcuteType.UPDATE || this.ExcuteType == DMSExcuteType.UPDATE_WHERE) { this._ParamSql.Append(text); this._strSql.Append(DMSOperators.FormatBinaryOperator(System.Linq.Expressions.ExpressionType.Equal)); } } return(base.VisitBinding(binding)); }