本文整理汇总了C#中System.Linq.Expressions.ConditionalExpression类的典型用法代码示例。如果您正苦于以下问题:C# ConditionalExpression类的具体用法?C# ConditionalExpression怎么用?C# ConditionalExpression使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConditionalExpression类属于System.Linq.Expressions命名空间,在下文中一共展示了ConditionalExpression类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SerializableConditionalExpression
/// <summary>
/// Constructor with an <see cref="ConditionalExpression"/> and an <see cref="ExpressionConverter"/>.
/// </summary>
/// <param name="expression">The original, not serializable <see cref="Expression"/>.</param>
/// <param name="expConverter">The <see cref="ExpressionConverter"/> to convert contained <see cref="Expression">Expressions</see>.</param>
public SerializableConditionalExpression(ConditionalExpression expression, ExpressionConverter expConverter)
: base(expression, expConverter)
{
IfTrue = expression.IfTrue.MakeSerializable(expConverter);
IfFalse = expression.IfFalse.MakeSerializable(expConverter);
Test = expression.Test.MakeSerializable(expConverter);
}
示例2: TryRemoveNullPropagation
private bool TryRemoveNullPropagation(ConditionalExpression node, out Expression condition)
{
condition = null;
if (!typeof(IEnumerable).IsAssignableFrom(node.Type)) {
return false;
}
if (node.Test.NodeType != ExpressionType.Equal) {
return false;
}
if (node.IfTrue.NodeType != ExpressionType.Call) {
return false;
}
if (node.IfFalse.NodeType != ExpressionType.MemberAccess) {
return false;
}
if (!node.Type.IsAssignableFrom(node.IfFalse.Type)) {
return false;
}
_yankingNull = true;
condition = node.IfFalse;
_yankingNull = false;
return true;
}
示例3: UpdateConditional
protected ConditionalExpression UpdateConditional(ConditionalExpression c, Expression test, Expression ifTrue, Expression ifFalse)
{
if (test != c.Test || ifTrue != c.IfTrue || ifFalse != c.IfFalse) {
return Expression.Condition(test, ifTrue, ifFalse);
}
return c;
}
示例4: TryRemoveNullPropagation
private bool TryRemoveNullPropagation(ConditionalExpression node, out Expression condition)
{
condition = null;
if (node.IfTrue.NodeType != ExpressionType.Constant) {
return false;
}
if (node.Test.NodeType != ExpressionType.Equal) {
return false;
}
var test = (BinaryExpression)node.Test;
var constantExpr = (ConstantExpression)node.IfTrue;
if (node.IfFalse.NodeType != ExpressionType.Call) {
return false;
}
var memberExpr = (MethodCallExpression)node.IfFalse;
_yankingNull = true;
condition = Visit(memberExpr);
_yankingNull = false;
return true;
}
示例5: VisitConditional
protected override Expression VisitConditional(ConditionalExpression node)
{
return Expression.Condition(
Visit(node.Test),
Visit(node.IfTrue),
Visit(node.IfFalse));
}
示例6: TryToCoalesceAsNullable
private Expression TryToCoalesceAsNullable(ConditionalExpression c)
{
var hasValueTarget = (Expression)null;
var getValueTarget = (Expression)null;
var matched = Matcher
.For(c.Test).AsPropertyOrField()
.Property(property => property.Name == "HasValue" && property.DeclaringType.IsGenericTypeDefinedAs(typeof(Nullable<>)))
.Do(p => hasValueTarget = p.Expression)
.For(c.IfTrue).AsConvert()
.Type(typeof(Nullable<>))
.Operand().AsMethodCall()
.Method(method => method.Name == "GetValueOrDefault"
&& method.DeclaringType.IsGenericTypeDefinedAs(typeof(Nullable<>)))
.Do(call => getValueTarget = call.Object)
.Matched;
if (!matched)
return null;
if (hasValueTarget != getValueTarget)
return null;
return Expression.Coalesce(getValueTarget, c.IfFalse);
}
示例7: VisitConditional
public override Expression VisitConditional(ConditionalExpression c)
{
_Builder.Append("(");
var conditional = base.VisitConditional(c);
_Builder.Append(")");
return conditional;
}
示例8: VisitConditional
protected override Expression VisitConditional(ConditionalExpression expression)
{
var test = this.Visit(expression.Test);
var ifFalse = this.Visit(expression.IfFalse);
var ifTrue = this.Visit(expression.IfTrue);
if (test is BitBooleanExpression)
{
test = Expression.Equal(test, Expression.Constant(true));
}
if (ifFalse.Type.GetUnwrappedNullableType() == typeof(bool) && !(ifFalse is BitBooleanExpression))
{
ifFalse = BitBooleanExpression.Coerce(ifFalse);
}
if (ifTrue.Type.GetUnwrappedNullableType() == typeof(bool) && !(ifTrue is BitBooleanExpression))
{
ifTrue = BitBooleanExpression.Coerce(ifTrue);
}
if (test != expression.Test || ifFalse != expression.IfFalse || ifTrue != expression.IfTrue)
{
return new BitBooleanExpression(Expression.Condition(test, ifTrue, ifFalse));
}
else
{
return base.VisitConditional(expression);
}
}
示例9: VisitConditional
protected override Expression VisitConditional(ConditionalExpression c)
{
c = (ConditionalExpression)base.VisitConditional(c);
return TryToCoalesceAsObjects(c)
?? TryToCoalesceAsNullable(c)
?? c;
}
示例10: UpdateConditional
protected static ConditionalExpression UpdateConditional(ConditionalExpression node, Expression test,
Expression ifTrue, Expression ifFalse)
{
if (node.Test != test || node.IfTrue != ifTrue || node.IfFalse != ifFalse)
{
return Expression.Condition(test, ifTrue, ifFalse);
}
return node;
}
示例11: HandleConditionalExpression
/*********
** Protected methods
*********/
/// <summary>Handles the conditional expression (equivalent to <c>.If {} .Else {}</c> in the sample expression tree in the <see cref="FixStringMethodsVisitor"/> remarks).</summary>
/// <param name="original">The original expression.</param>
/// <param name="ifElse">The conditional expression.</param>
/// <returns>A reduced if/else statement if it contains any of the matched methods. Otherwise, the original expression.</returns>
private Expression HandleConditionalExpression(Expression original, ConditionalExpression ifElse)
{
var elseExpression = ifElse.IfFalse as UnaryExpression;
if (elseExpression != null)
{
var methodCallExpression = elseExpression.Operand as MethodCallExpression;
if (methodCallExpression != null)
{
if (this.BooleanReturnStringMethods.Contains(methodCallExpression.Method))
{
var methodCallReplacement = Expression.Call(
methodCallExpression.Object,
methodCallExpression.Method,
methodCallExpression.Arguments);
// Convert the result to a nullable boolean so the Expression.Equal works.
var result = Expression.Convert(methodCallReplacement, typeof(bool?));
return result;
}
if (this.IntegerStringMethods.Contains(methodCallExpression.Method))
{
var methodCallReplacement = Expression.Call(
methodCallExpression.Object,
methodCallExpression.Method,
methodCallExpression.Arguments);
var result = Expression.Convert(methodCallReplacement, typeof(int?));
return result;
}
}
}
var firstLevelMethodCallExpression = ifElse.IfFalse as MethodCallExpression;
if (firstLevelMethodCallExpression != null)
{
// Using the method name and declaring type as strings because I don't want to add a dependency to the project for a simple check like that.
if (firstLevelMethodCallExpression.Method.DeclaringType != null &&
firstLevelMethodCallExpression.Method.DeclaringType.FullName == "System.Web.Http.OData.Query.Expressions.ClrSafeFunctions" &&
(firstLevelMethodCallExpression.Method.Name == "SubstringStartAndLength" || firstLevelMethodCallExpression.Method.Name == "SubstringStart"))
{
var arguments = firstLevelMethodCallExpression.Arguments.Skip(1).ToArray();
return Expression.Call(
firstLevelMethodCallExpression.Arguments[0],
typeof(string).GetMethod("Substring", arguments.Select(x => typeof(int)).ToArray()),
arguments);
}
if (this.ConcatStringMethods.Contains(firstLevelMethodCallExpression.Method))
{
return Expression.Add(firstLevelMethodCallExpression.Arguments.First(), firstLevelMethodCallExpression.Arguments.Last(), firstLevelMethodCallExpression.Method);
}
}
return original;
}
示例12: VisitConditional
protected override Expression VisitConditional(ConditionalExpression node)
{
DefaultExpression defaultExpression = node.IfFalse as DefaultExpression;
if (defaultExpression == null || defaultExpression.Type != typeof (void) || !_canReplace)
return base.VisitConditional(node);
_canReplace = false;
return Visit(Expression.IfThenElse(node.Test, node.IfTrue, Replacement));
}
示例13: Conditional
void Conditional(ConditionalExpression expression)
{
Write("Conditional:");
Write("Test:");
Expression(expression.Test);
Write("IfTrue:");
Expression(expression.IfTrue);
Write("IfFalse:");
Expression(expression.IfFalse);
}
示例14: VisitConditional
protected override Expression VisitConditional(ConditionalExpression node)
{
var cond = Visit(node.Test);
AddParameter(cond);
var lhs = Visit(node.IfTrue);
AddParameter(lhs);
var rhs = Visit(node.IfFalse);
AddParameter(rhs);
return null;
}
示例15: VisitConditional
protected override Expression VisitConditional(ConditionalExpression expression)
{
var constantExpression = expression.Test as ConstantExpression;
if (constantExpression != null)
{
return Convert.ToBoolean(constantExpression.Value) ? expression.IfTrue : expression.IfFalse;
}
return base.VisitConditional(expression);
}