本文整理汇总了C#中Expression类的典型用法代码示例。如果您正苦于以下问题:C# Expression类的具体用法?C# Expression怎么用?C# Expression使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Expression类属于命名空间,在下文中一共展示了Expression类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConversionToLessSpecficWithBoxingToObject
public void ConversionToLessSpecficWithBoxingToObject()
{
Expression<object> expr = new Expression<object>("2 + 2");
object result = expr.Evaluate();
Assert.AreEqual(4, result);
}
示例2: MethodCall
public MethodCall(Expression target, string/*!*/ methodName, Arguments args, Block block, SourceSpan location)
: base(args, block, location) {
Assert.NotEmpty(methodName);
_methodName = methodName;
_target = target;
}
示例3: Coalesce
public static Expression Coalesce(LambdaBuilder builder, Expression left, Expression right)
{
ParameterExpression temp;
Expression result = Coalesce(left, right, out temp);
builder.AddHiddenVariable(temp);
return result;
}
示例4: UnaryExpression
public UnaryExpression(Terminal op, Expression expression)
{
Operator = op;
Expression = expression;
AddAttribute(new OperatorAttr(op));
AddChild(expression);
}
示例5: Collect
public void Collect(Expression tvBasePointer, int basePointerSize, Expression eField, Expression effectiveAddress)
{
this.basePointer = tvBasePointer;
this.basePointerSize = basePointerSize;
this.eField = eField;
effectiveAddress.Accept(this);
}
示例6: ComInvokeBinder
internal ComInvokeBinder(
CallInfo callInfo,
DynamicMetaObject[] args,
bool[] isByRef,
BindingRestrictions restrictions,
Expression method,
Expression dispatch,
ComMethodDesc methodDesc
)
{
Debug.Assert(callInfo != null, "arguments");
Debug.Assert(args != null, "args");
Debug.Assert(isByRef != null, "isByRef");
Debug.Assert(method != null, "method");
Debug.Assert(dispatch != null, "dispatch");
Debug.Assert(TypeUtils.AreReferenceAssignable(typeof(ComMethodDesc), method.Type), "method");
Debug.Assert(TypeUtils.AreReferenceAssignable(typeof(IDispatch), dispatch.Type), "dispatch");
_method = method;
_dispatch = dispatch;
_methodDesc = methodDesc;
_callInfo = callInfo;
_args = args;
_isByRef = isByRef;
_restrictions = restrictions;
// Set Instance to some value so that CallBinderHelper has the right number of parameters to work with
_instance = dispatch;
}
示例7: EmitExpressionAsVoid
/// <summary>
/// Emits an expression and discards the result. For some nodes this emits
/// more optimial code then EmitExpression/Pop
/// </summary>
private void EmitExpressionAsVoid(Expression node) {
Debug.Assert(node != null);
ExpressionStart startEmitted = EmitExpressionStart(node);
switch (node.NodeType) {
case ExpressionType.Assign:
EmitAssign((BinaryExpression)node, EmitAs.Void);
break;
case ExpressionType.Block:
Emit((BlockExpression)node, EmitAs.Void);
break;
case ExpressionType.Throw:
EmitThrow((UnaryExpression)node, EmitAs.Void);
break;
case ExpressionType.Constant:
case ExpressionType.Default:
case ExpressionType.Parameter:
// no-op
break;
default:
EmitExpression(node, false);
if (node.Type != typeof(void)) {
_ilg.Emit(OpCodes.Pop);
}
break;
}
EmitExpressionEnd(startEmitted);
}
示例8: SetParent
public override void SetParent(Expression parent)
{
base.SetParent(parent);
if (Value != null)
Value.SetParent(this);
}
示例9: Equals
public override bool Equals(Expression E)
{
Product P = E as Product;
if (ReferenceEquals(P, null)) return base.Equals(E);
return Terms.SequenceEqual(P.Terms);
}
示例10: SimpleCallHelper
/// <summary>
/// The helper to create the AST method call node. Will add conversions (Utils.Convert)
/// to parameters and instance if necessary.
/// </summary>
public static MethodCallExpression SimpleCallHelper(Expression instance, MethodInfo method, params Expression[] arguments) {
ContractUtils.RequiresNotNull(method, "method");
ContractUtils.Requires(instance != null ^ method.IsStatic, "instance");
ContractUtils.RequiresNotNullItems(arguments, "arguments");
ParameterInfo[] parameters = method.GetParameters();
ContractUtils.Requires(arguments.Length == parameters.Length, "arguments", "Incorrect number of arguments");
if (instance != null) {
instance = Convert(instance, method.DeclaringType);
}
Expression[] convertedArguments = ArgumentConvertHelper(arguments, parameters);
ReadOnlyCollection<Expression> finalArgs;
if (convertedArguments == arguments) {
// we didn't convert anything, just convert the users original
// array to a readonly collection.
finalArgs = convertedArguments.ToReadOnly();
} else {
// we already copied the array so just stick it in a readonly collection.
finalArgs = new ReadOnlyCollection<Expression>(convertedArguments);
}
// the arguments are now all correct, avoid re-validating the call parameters and
// directly create the expression.
return Expression.Call(instance, method, finalArgs);
}
示例11: ArgumentConvertHelper
private static Expression[] ArgumentConvertHelper(Expression[] arguments, ParameterInfo[] parameters) {
Debug.Assert(arguments != null);
Debug.Assert(arguments != null);
Expression[] clone = null;
for (int arg = 0; arg < arguments.Length; arg++) {
Expression argument = arguments[arg];
if (!CompatibleParameterTypes(parameters[arg].ParameterType, argument.Type)) {
// Clone the arguments array if needed
if (clone == null) {
clone = new Expression[arguments.Length];
// Copy the expressions into the clone
for (int i = 0; i < arg; i++) {
clone[i] = arguments[i];
}
}
argument = ArgumentConvertHelper(argument, parameters[arg].ParameterType);
}
if (clone != null) {
clone[arg] = argument;
}
}
return clone ?? arguments;
}
示例12: ShouldSolveSystemWith1EquationAnd2Variables
public void ShouldSolveSystemWith1EquationAnd2Variables()
{
//x+y=10 x=6; y=4
//x-y=2
var variableX = new Variable("x");
var variableY = new Variable("y");
var left1FirstEq = new Expression(1, variableX);
var left2FirstEq = new Expression(1, variableY);
var rightFirstEq = new Expression(10, Variable.NULL);
var left1SecondEq = new Expression(1, variableX);
var left2SecondEq = new Expression(-1, variableY);
var rightSecondEq = new Expression(2, Variable.NULL);
var firstEquation = new Equation(new List<Expression>() { left1FirstEq, left2FirstEq }, rightFirstEq);
var secondEquation = new Equation(new List<Expression>() {left1SecondEq, left2SecondEq}, rightSecondEq);
var target = new SystemOfEquations(new List<Equation>() {firstEquation, secondEquation});
target.Solve();
var resultX = variableX.Value;
var resultY = variableY.Value;
Assert.AreEqual(1, resultX.Count);
Assert.AreEqual(1, resultY.Count);
Assert.AreEqual(Variable.NULL, resultX.First().Variable);
Assert.AreEqual(6, resultX.First().Coefficient);
Assert.AreEqual(Variable.NULL, resultY.First().Variable);
Assert.AreEqual(4, resultY.First().Coefficient);
}
示例13: Binary
public Binary(SourceSpan span, Operator op, Expression left, Expression right)
: base(span)
{
_op = op;
_left = left;
_right = right;
}
示例14: ForLoopStatement
public ForLoopStatement(ScriptLoadingContext lcontext, Token nameToken, Token forToken)
: base(lcontext)
{
// for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end |
// lexer already at the '=' ! [due to dispatching vs for-each]
CheckTokenType(lcontext, TokenType.Op_Assignment);
m_Start = Expression.Expr(lcontext);
CheckTokenType(lcontext, TokenType.Comma);
m_End = Expression.Expr(lcontext);
if (lcontext.Lexer.Current.Type == TokenType.Comma)
{
lcontext.Lexer.Next();
m_Step = Expression.Expr(lcontext);
}
else
{
m_Step = new LiteralExpression(lcontext, DynValue.NewNumber(1));
}
lcontext.Scope.PushBlock();
m_VarName = lcontext.Scope.DefineLocal(nameToken.Text);
m_RefFor = forToken.GetSourceRef(CheckTokenType(lcontext, TokenType.Do));
m_InnerBlock = new CompositeStatement(lcontext);
m_RefEnd = CheckTokenType(lcontext, TokenType.End).GetSourceRef();
m_StackFrame = lcontext.Scope.PopBlock();
lcontext.Source.Refs.Add(m_RefFor);
lcontext.Source.Refs.Add(m_RefEnd);
}
示例15: BinaryOperationExpressionCombinesSequences
public void BinaryOperationExpressionCombinesSequences()
{
var a = new Expression[] { 1, 2, null, "Hello", JS.Function().Call() };
var c = a.Combined(BinaryOperator.Add);
Assert.AreEqual("1+2+null+\"Hello\"+(function(){})();", c.ToString());
}