本文整理汇总了C#中System.Linq.Expressions.Expression.MaybeAs方法的典型用法代码示例。如果您正苦于以下问题:C# Expression.MaybeAs方法的具体用法?C# Expression.MaybeAs怎么用?C# Expression.MaybeAs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Linq.Expressions.Expression
的用法示例。
在下文中一共展示了Expression.MaybeAs方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Evaluate
public static Maybe<object> Evaluate(Expression argument)
{
var value = argument.MaybeAs<ConstantExpression>().Select(x => x.Value)
.OrElse(() =>
from memberExpression in argument.MaybeAs<MemberExpression>()
from @object in memberExpression.Expression.MaybeAs<ConstantExpression>(nullMeansNothing: false)
from computedValue in
memberExpression.Member.MaybeAs<PropertyInfo>().Select(x => x.GetValue(@object.Value, null))
.OrElse(() => memberExpression.Member.MaybeAs<FieldInfo>().Select(x => x.GetValue(@object.Value)))
select computedValue
);
return value;
}
示例2: SimpleEval
public static Maybe<object> SimpleEval(Expression lambdaContainer)
{
Maybe<object> lambda = from methodCallExpression in lambdaContainer.MaybeAs<MethodCallExpression>()
from @object in methodCallExpression.Object.MaybeAs<ConstantExpression>(nullMeansNothing: false)
from parameters in GetParameters(methodCallExpression)
from lambdaExpression in Maybe.Just(methodCallExpression.Method.Invoke(@object == null ? null : @object.Value, parameters.ToArray()))
select lambdaExpression;
Maybe<object> maybe = lambda.OrElse(() => Evaluate(lambdaContainer));
return maybe;
}
示例3: GetMethodName
private static Maybe<string> GetMethodName(Expression expr)
{
return expr.MaybeAs<MethodCallExpression>().Bind(
call =>
{
if (call.Arguments.Count == 0 || (call.Method.IsExtensionMethod() && call.Arguments.Count == 1))
{
return Maybe.Just(call.Method.Name);
}
return Maybe.Nothing;
}
);
}
示例4: GetConditionName
// special case for protected nullable properties
// for x==null?null:(Nullable<T>)Expr(x) returns name of Expr(x)
private Maybe<string> GetConditionName(Expression expr, int exprPos)
{
return expr.MaybeAs<ConditionalExpression>()
.Bind(conditional => ExtensionsProvider.GetValue<Expression>(conditional)
.Bind(x => GetPropertyName(x, exprPos)));
}
示例5: GetEnumerableRootName
// find the root of Enumerable chains and get it name
// userstories.Where(x).select(y)=>'userstories'
private Maybe<string> GetEnumerableRootName(Expression expr, int exprPos)
{
return expr.MaybeAs<MethodCallExpression>()
.Where(x => x.Method.DeclaringType == typeof(Enumerable))
.Where(x => !AggregationMethodNames.Contains(x.Method.Name))
.Select(x => x.Arguments.First())
.Bind(x => GetPropertyName(x, exprPos));
}
示例6: GetPropertyName
private Maybe<string> GetPropertyName(Expression expr, int exprPos)
{
var maybePropName = expr
.MaybeAs<MemberExpression>().Select(x => x.Member.Name)
.OrElse(() => DynamicDictionary.GetAlias(expr, exprPos))
.OrElse(() => GetConditionName(expr, exprPos))
.OrElse(() => GetEnumerableRootName(expr, exprPos))
.OrElse(() => GetMethodName(expr));
return maybePropName;
}