本文整理汇总了C#中System.Linq.Expressions.Expression.Convert方法的典型用法代码示例。如果您正苦于以下问题:C# Expression.Convert方法的具体用法?C# Expression.Convert怎么用?C# Expression.Convert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Linq.Expressions.Expression
的用法示例。
在下文中一共展示了Expression.Convert方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReplaceParameter
Expression ReplaceParameter(IDictionary<Expression,Expression> expressionAccessors, Expression expression, Action<string> setName)
{
return expression.Convert(expr =>
{
if (expr.NodeType == ExpressionType.Constant)
{
var c = (ConstantExpression)expr;
if (!ExpressionHelper.IsConstant(expr.Type) || _asParameters.Contains(c))
{
var val = expressionAccessors[expr];
expr = Expression.Convert(val, expr.Type);
if (expression.NodeType == ExpressionType.MemberAccess)
{
var ma = (MemberExpression)expression;
setName(ma.Member.Name);
}
}
}
return expr;
});
}
示例2: BuildExpression
public Expression BuildExpression(IBuildContext context, Expression expression)
{
var newExpr = expression.Convert(pi =>
{
switch (pi.NodeType)
{
case ExpressionType.MemberAccess:
{
if (IsServerSideOnly(pi) || PreferServerSide(pi))
return BuildSql(context, pi);
var ma = (MemberExpression)pi;
if (ConvertMember(ma.Member) != null)
break;
var ctx = GetContext(context, pi);
if (ctx != null)
return ctx.BuildExpression(pi, 0);
var ex = ma.Expression;
if (ex != null && ex.NodeType == ExpressionType.Constant)
{
// field = localVariable
//
var c = _expressionAccessors[ex];
return Expression.MakeMemberAccess(Expression.Convert(c, ex.Type), ma.Member);
}
break;
}
case ExpressionType.Parameter:
{
if (pi == ParametersParam)
break;
var ctx = GetContext(context, pi);
if (ctx != null)
return ctx.BuildExpression(pi, 0);
throw new NotImplementedException();
}
case ExpressionType.Constant:
{
if (ExpressionHelper.IsConstant(pi.Type))
break;
if (_expressionAccessors.ContainsKey(pi))
return Expression.Convert(_expressionAccessors[pi], pi.Type);
throw new NotImplementedException();
}
case ExpressionType.Coalesce:
if (pi.Type == typeof(string) && MappingSchema.GetDefaultNullValue<string>() != null)
return BuildSql(context, pi);
if (CanBeTranslatedToSql(context, ConvertExpression(pi), true))
return BuildSql(context, pi);
break;
case ExpressionType.Conditional:
if (CanBeTranslatedToSql(context, ConvertExpression(pi), true))
return BuildSql(context, pi);
break;
case ExpressionType.Call:
{
var ce = (MethodCallExpression)pi;
var cm = ConvertMethod(ce);
if (cm != null)
if (ce.Method.GetCustomAttributes(typeof(MethodExpressionAttribute), true).Length != 0)
return BuildExpression(context, cm);
if (IsSubQuery(context, ce))
return GetSubQuery(context, ce).BuildExpression(null, 0);
if (IsServerSideOnly(pi) || PreferServerSide(pi))
return BuildSql(context, pi);
}
break;
}
if (EnforceServerSide(context))
{
switch (pi.NodeType)
{
case ExpressionType.MemberInit:
case ExpressionType.New:
case ExpressionType.Convert:
//.........这里部分代码省略.........
示例3: ParseSubQuery
ISqlExpression ParseSubQuery(IParseContext context, Expression expression)
{
if (expression.NodeType == ExpressionType.MemberAccess)
{
var ma = (MemberExpression)expression;
if (ma.Expression != null)
{
switch (ma.Expression.NodeType)
{
case ExpressionType.Call :
case ExpressionType.MemberAccess :
case ExpressionType.Parameter :
{
var ctx = GetSubQuery(context, ma.Expression);
var ex = expression.Convert(e => e == ma.Expression ? Expression.Constant(null, ma.Expression.Type) : e);
var sql = ctx.ConvertToSql(ex, 0, ConvertFlags.Field);
if (sql.Length != 1)
throw new NotImplementedException();
ctx.SqlQuery.Select.Add(sql[0].Sql);
var idx = context.SqlQuery.Select.Add(ctx.SqlQuery);
return context.SqlQuery.Select.Columns[idx];
}
}
}
}
var sequence = GetSubQuery(context, expression);
/*
if (expr.NodeType == ExpressionType.Call)
{
var call = (MethodCallExpression)expr;
if (call.Method.Name == "Any" && (call.Method.DeclaringType == typeof(Queryable) || call.Method.DeclaringType == typeof(Enumerable)))
return ((SqlQuery.Predicate.FuncLike) result.Where.SearchCondition.Conditions[0].Predicate).Function;
}
*/
return sequence.SqlQuery;
}
示例4: ConvertValue
internal static Expression ConvertValue(TypeConstraintAst typeConstraint, Expression expr)
{
var typeName = typeConstraint.TypeName;
var toType = typeName.GetReflectionType();
if (toType != null)
{
if (toType == typeof(void))
{
return Expression.Block(typeof(void), expr);
}
return expr.Convert(toType);
}
// typeName can't be resolved at compile time, so defer resolution until runtime.
return DynamicExpression.Dynamic(PSDynamicConvertBinder.Get(), typeof(object),
Expression.Call(CachedReflectionInfo.TypeOps_ResolveTypeName,
Expression.Constant(typeName),
Expression.Constant(typeName.Extent)),
expr);
}
示例5: typeof
Expression IAssignableValue.SetValue(Compiler compiler, Expression rhs)
{
IEnumerable<PropertyInfo> enumerable;
bool flag;
if (this.VariablePath.IsVariable && this.VariablePath.UnqualifiedPath.Equals("null", StringComparison.OrdinalIgnoreCase))
{
return rhs;
}
Type type = this.GetVariableType(compiler, out enumerable, out flag);
Type type2 = rhs.Type;
if ((flag && (type.Equals(typeof(object)) || type.Equals(typeof(PSObject)))) && (type2.Equals(typeof(object)) || type2.Equals(typeof(PSObject))))
{
rhs = Expression.Dynamic(PSVariableAssignmentBinder.Get(), typeof(object), rhs);
}
rhs = rhs.Convert(type);
if (!flag)
{
return Compiler.CallSetVariable(Expression.Constant(this.VariablePath), rhs, null);
}
Expression localVariablesParameter = compiler.LocalVariablesParameter;
foreach (PropertyInfo info in enumerable)
{
localVariablesParameter = Expression.Property(localVariablesParameter, info);
}
return Expression.Assign(localVariablesParameter, rhs);
}
示例6: ConvertValue
internal static Expression ConvertValue(ITypeName typeName, Expression expr)
{
Type reflectionType = typeName.GetReflectionType();
if (reflectionType == null)
{
return Expression.Dynamic(PSDynamicConvertBinder.Get(), typeof(object), Expression.Call(CachedReflectionInfo.TypeOps_ResolveTypeName, Expression.Constant(typeName)), expr);
}
if (reflectionType.Equals(typeof(void)))
{
return Expression.Block(typeof(void), new Expression[] { expr });
}
return expr.Convert(reflectionType);
}
示例7: BuildSubQuery
Expression BuildSubQuery(IParseContext context, Expression expression)
{
if (expression.NodeType == ExpressionType.MemberAccess)
{
var ma = (MemberExpression)expression;
if (ma.Expression != null)
{
switch (ma.Expression.NodeType)
{
case ExpressionType.Call :
case ExpressionType.MemberAccess :
case ExpressionType.Parameter :
{
var ctx = GetSubQuery(context, ma.Expression);
var ex = expression.Convert(e => e == ma.Expression ? Expression.Constant(null, ma.Expression.Type) : e);
var sql = ctx.ConvertToSql(ex, 0, ConvertFlags.Field);
if (sql.Length != 1)
throw new NotImplementedException();
//ctx.SqlQuery.Select.Columns.Clear();
ctx.SqlQuery.Select.Add(sql[0].Sql);
var idx = context.SqlQuery.Select.Add(ctx.SqlQuery);
return BuildSql(expression.Type, idx);
}
}
}
}
var sequence = GetSubQuery(context, expression);
return sequence.BuildExpression(null, 0);
throw new NotImplementedException();
/*
ParentQueries.Insert(0, new ParentQuery { Parent = query.BaseQuery, Parameter = query.Lambda.Parameters[0]});
var sql = CurrentSql;
CurrentSql = new SqlQuery { ParentSql = sql };
var prev = _isSubQueryParsing;
_isSubQueryParsing = true;
var seq = ParseSequence(expr)[0];
_isSubQueryParsing = prev;
if (seq.Fields.Count == 1 && CurrentSql.Select.Columns.Count == 0)
seq.Fields[0].Select(this);
var column = new QueryField.ExprColumn(query, CurrentSql, null);
query.Fields.Add(column);
var idx = column.Select(this);
var result = BuildField(expr, column.GetExpressions(this)[0], idx.Select(i => converter(i).Index).ToArray());
CurrentSql = sql;
ParentQueries.RemoveAt(0);
ParsingTracer.DecIndentLevel();
return result;
*/
}
示例8: BuildExpression
public Expression BuildExpression(IParseContext context, Expression expression)
{
var newExpr = expression.Convert(pi =>
{
switch (pi.NodeType)
{
case ExpressionType.MemberAccess:
{
if (IsSubQuery(context, pi))
return BuildSubQuery(context, pi);
if (IsServerSideOnly(pi) || PreferServerSide(pi))
return BuildSql(context, pi);
var ctx = GetContext(context, pi);
if (ctx != null)
return ctx.BuildExpression(pi, 0);
var ma = (MemberExpression)pi;
var ex = ma.Expression;
/*
if (query.Sources.Length > 0)
{
var field = query.GetBaseField(lambda, ma);
if (field != null)
{
if (field is QueryField.Column)
return BuildField(ma, field, converter);
if (field is QuerySource.SubQuery)
return BuildSubQuerySource(ma, (QuerySource.SubQuery)field, converter);
if (field is QueryField.ExprColumn)
{
var col = (QueryField.ExprColumn)field;
return BuildNewExpression(lambda, col.QuerySource, col.Expr, converter);
}
if (field is QuerySource.Table)
return BuildTable(ma, (QuerySource.Table)field, null, converter);
if (field is QueryField.SubQueryColumn)
return BuildSubQuerySource(ma, (QueryField.SubQueryColumn)field, converter);
if (field is QueryField.GroupByColumn)
return BuildGroupBy(ma, (QueryField.GroupByColumn)field, converter);
if (field is QuerySource.SubQuerySourceColumn)
return BuildSubQuerySourceColumn(pi, (QuerySource.SubQuerySourceColumn)field, converter);
throw new InvalidOperationException();
}
//if (ex.Expr == expr.Expr && query is QuerySource.Scalar && ex.NodeType == ExpressionType.Constant)
// return BuildField(lambda, query, ma);
}
else
{
var field = GetField(lambda, ma, query);
if (field != null)
return BuildField(ma, field, converter/*i => i/);
}
*/
if (ex != null && ex.NodeType == ExpressionType.Constant)
{
// field = localVariable
//
var c = _expressionAccessors[ex];
return Expression.MakeMemberAccess(Expression.Convert(c, ex.Type), ma.Member);
}
break;
}
case ExpressionType.Parameter:
{
if (pi == ParametersParam)
break;
var ctx = GetContext(context, pi);
if (ctx != null)
return ctx.BuildExpression(pi, 0);
throw new NotImplementedException();
/*
if (query.Lambda != null &&
query.Lambda.MethodInfo != null &&
query.Lambda.MethodInfo.Name == "Select" &&
query.Lambda.Parameters.Length == 2 &&
query.Lambda.Parameters[1] == pi)
{
return Expression.MakeMemberAccess(_contextParam, QueryCtx.Counter);
}
//.........这里部分代码省略.........