ArrayIndex() 공개 정적인 메소드

Creates a BinaryExpression that represents applying an array index operator to an array of rank one.
public static ArrayIndex ( Expression array, Expression index ) : BinaryExpression
array Expression An to set the property equal to.
index Expression An to set the property equal to.
리턴 BinaryExpression
예제 #1
0
        public void ArrayIndex_index()
        {
            var expected = LinqExpression.ArrayIndex(
                LinqExpression.Parameter(
                    typeof(int[])),
                LinqExpression.Parameter(
                    typeof(int)));

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    :arrayIndexArray [
        :parameterType [
            :typeName ""System.Int32[]"" ;
        ] ;
    ] ;
    :arrayIndexIndex [
        :parameterType [
            :typeName ""System.Int32"" ;
        ] ;
    ] ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
예제 #2
0
        // Pseudocode:
        //string input =>
        //{
        //    R result = 0;
        //    for (int i = input.Length - 1; i >= 0; i--)
        //    {
        //        result <<= 6;
        //        var m = _invMap[input[i]];
        //        if (m == 0xff)
        //            return default(ConversionResult<R>);
        //        result += m;
        //    }
        //    return new ConversionResult<R>(result);
        //}
        private LambdaExpression fromLambda(Type to)
        {
            var stringthis = typeof(string).GetTypeInfo().DeclaredProperties.First(p => p.GetIndexParameters().Length == 1 && p.GetIndexParameters()[0].ParameterType == typeof(int));
            var input      = Ex.Parameter(typeof(string), "input");
            var result     = Ex.Parameter(to, "result");
            var i          = Ex.Parameter(typeof(int), "i");
            var m          = Ex.Parameter(typeof(byte), "m");
            var loopstart  = Ex.Label("loopstart");
            var end        = Ex.Label(typeof(ConversionResult <>).MakeGenericType(to), "end");
            var loop       = Ex.Block(
                Ex.Label(loopstart),
                Ex.IfThen(Ex.MakeBinary(ExpressionType.LessThan, i, Ex.Constant(0)),
                          Ex.Goto(end, Result(to, result))),
                Ex.LeftShiftAssign(result, Ex.Constant(6)),
                Ex.Assign(m, Ex.ArrayIndex(Ex.Constant(_invMap), Ex.Convert(Ex.MakeIndex(input, stringthis, new[] { i }), typeof(int)))),
                Ex.IfThen(Ex.MakeBinary(ExpressionType.Equal, m, Ex.Constant((byte)0xff)),
                          Ex.Goto(end, NoResult(to))),
                Ex.AddAssign(result, Ex.Convert(m, result.Type)),
                Ex.PostDecrementAssign(i),
                Ex.Goto(loopstart));
            var block = Ex.Block(new[] { result, i, m },
                                 Ex.Assign(result, Ex.Convert(Ex.Constant(0), to)),
                                 Ex.Assign(i, Ex.MakeBinary(ExpressionType.Subtract, Ex.Property(input, nameof(string.Length)), Ex.Constant(1))),
                                 loop,
                                 Ex.Label(end, NoResult(to)));

            return(Ex.Lambda(block, input));
        }
예제 #3
0
        internal static Expression InjectedCallExpr(ParameterInfo[] initParams, Expression meta, Expression persistVar, Func <IEnumerable <Expression>, Expression> exprGen)
        {
            var arr = ExpressionEx.Variable(typeof(object[]), "initArr");

            return(ExpressionEx.Block(new[] { arr },
                                      ExpressionEx.Assign(arr, Expression.Call(InjectMethod, Expression.Constant(initParams), meta, persistVar)),
                                      exprGen(initParams
                                              .Select(p => p.ParameterType)
                                              .Select((t, i) => (Expression)Expression.Convert(
                                                          Expression.ArrayIndex(arr, Expression.Constant(i)), t)))));
        }
        public void ArrayIndex_index()
        {
            var expression =
                LinqExpression.ArrayIndex(
                    LinqExpression.Parameter(
                        typeof(int[])),
                    LinqExpression.Parameter(
                        typeof(int)));

            ShouldRoundrip(expression);
        }
        private MethodCallExpression MethodCallExpression(ExpressionType nodeType, System.Type type, JObject obj)
        {
            var instance  = this.Prop(obj, "object", this.Expression);
            var method    = this.Prop(obj, "method", this.Method);
            var arguments = this.Prop(obj, "arguments", this.Enumerable(this.Expression));

            switch (nodeType)
            {
            case ExpressionType.ArrayIndex:
                return(Expr.ArrayIndex(instance, arguments));

            case ExpressionType.Call:
                return(Expr.Call(instance, method, arguments));

            default:
                throw new NotSupportedException();
            }
        }
예제 #6
0
        // Pseudocode:
        //I input =>
        //{
        //  var i = (UI)input;
        //  var result = new char[base64sizeof(UI)];
        //  for (int j = 0; j == 0 || i > 0; j++)
        //  {
        //      result[j] = _mapChar[i & 0x3f];
        //      i >>= 6;
        //  }
        //}
        private LambdaExpression toLambda(Type from)
        {
            var input     = Ex.Parameter(from, "input");
            var result    = Ex.Parameter(typeof(char[]), "result");
            var i         = workingType(from) == from ? input : Ex.Parameter(workingType(from), "i");
            var j         = Ex.Parameter(typeof(int), "j");
            var loopstart = Ex.Label("loopstart");
            var loopend   = Ex.Label("loopend");

            var loop = Ex.Block(
                Ex.Label(loopstart),
                Ex.IfThen(Ex.MakeBinary(ExpressionType.AndAlso,
                                        Ex.MakeBinary(ExpressionType.GreaterThan, j, Ex.Constant(0)),
                                        i.Type == typeof(BigInteger)
                        ? (Ex)Ex.Call(i, nameof(BigInteger.Equals), Type.EmptyTypes, Ex.Constant(BigInteger.Zero))
                        : Ex.MakeBinary(ExpressionType.Equal, i, Ex.Convert(Ex.Constant(0), i.Type))),
                          Ex.Goto(loopend)),
                Ex.Assign(
                    Ex.ArrayAccess(result, j),
                    Ex.ArrayIndex(Ex.Constant(_mapChars),
                                  Ex.Convert(Ex.MakeBinary(ExpressionType.And, i, Ex.Convert(Ex.Constant(0x3f), i.Type)), typeof(int)))),
                Ex.RightShiftAssign(i, Ex.Constant(6)),
                Ex.PostIncrementAssign(j),
                Ex.Goto(loopstart));
            var ret = Result(typeof(string),
                             Ex.New(typeof(string).GetTypeInfo().DeclaredConstructors
                                    .Select(c => new { c, p = c.GetParameters() })
                                    .First(c => c.p.Length == 3 && c.p[0].ParameterType == typeof(char[]) && c.p[1].ParameterType == typeof(int) && c.p[2].ParameterType == typeof(int)).c,
                                    result, Ex.Constant(0), j));
            var block = Ex.Block(Ex.Assign(j, Ex.Constant(0)),
                                 Ex.Assign(result, Ex.NewArrayBounds(typeof(char), Ex.Constant(charbound(from)))),
                                 loop,
                                 Ex.Label(loopend),
                                 ret);

            block = input == i
                ? Ex.Block(new[] { j, result },
                           block)
                : Ex.Block(new[] { i, j, result },
                           Ex.Assign(i, Ex.Convert(input, i.Type)),
                           block);

            return(Ex.Lambda(block, input));
        }
예제 #7
0
        public static ObjectActivator <T> GetActivator <T>(ConstructorInfo ctor)
        {
            Type type = ctor.DeclaringType;

            ParameterInfo[] paramsInfo = ctor.GetParameters();

            //create a single param of type object[]
            ParameterExpression param =
                Expression.Parameter(typeof(object[]), "args");

            Expression[] argsExp =
                new Expression[paramsInfo.Length];

            //pick each arg from the params array
            //and create a typed expression of them
            for (int i = 0; i < paramsInfo.Length; i++)
            {
                Expression index     = Expression.Constant(i);
                Type       paramType = paramsInfo[i].ParameterType;

                Expression paramAccessorExp =
                    Expression.ArrayIndex(param, index);

                Expression paramCastExp =
                    Expression.Convert(paramAccessorExp, paramType);

                argsExp[i] = paramCastExp;
            }

            //make a NewExpression that calls the
            //ctor with the args we just created
            NewExpression newExp = Expression.New(ctor, argsExp);

            //create a lambda with the New
            //Expression as body and our param object[] as arg
            LambdaExpression lambda =
                Expression.Lambda(typeof(ObjectActivator <T>), newExp, param);

            //compile it
            ObjectActivator <T> compiled = (ObjectActivator <T>)lambda.Compile();

            return(compiled);
        }
예제 #8
0
        public static System.Linq.Expressions.Expression <Func <object[], object> > CreateComplexExpression(string p = null)
        {
            var stateParamExpr = SysExpr.Parameter(typeof(object[]), p);

            var expr = SysExpr.Lambda <Func <object[], object> >(
                SysExpr.MemberInit(
                    SysExpr.New(_ctorOfA,
                                SysExpr.New(_ctorOfB),
                                SysExpr.Convert(SysExpr.ArrayIndex(stateParamExpr, SysExpr.Constant(11)), typeof(string)),
                                SysExpr.NewArrayInit(typeof(ID),
                                                     SysExpr.New(_ctorOfD1),
                                                     SysExpr.New(_ctorOfD2))),
                    SysExpr.Bind(_propAProp,
                                 SysExpr.New(_ctorOfP,
                                             SysExpr.New(_ctorOfB))),
                    SysExpr.Bind(_fieldABop,
                                 SysExpr.New(_ctorOfB))),
                stateParamExpr);

            return(expr);
        }
        private BinaryExpression BinaryExpression(
            ExpressionType nodeType, System.Type type, JObject obj)
        {
            var left       = this.Prop(obj, "left", this.Expression);
            var right      = this.Prop(obj, "right", this.Expression);
            var method     = this.Prop(obj, "method", this.Method);
            var conversion = this.Prop(obj, "conversion", this.LambdaExpression);
            var liftToNull = this.Prop(obj, "liftToNull").Value <bool>();

            switch (nodeType)
            {
            case ExpressionType.Add: return(Expr.Add(left, right, method));

            case ExpressionType.AddAssign: return(Expr.AddAssign(left, right, method, conversion));

            case ExpressionType.AddAssignChecked: return(Expr.AddAssignChecked(left, right, method, conversion));

            case ExpressionType.AddChecked: return(Expr.AddChecked(left, right, method));

            case ExpressionType.And: return(Expr.And(left, right, method));

            case ExpressionType.AndAlso: return(Expr.AndAlso(left, right, method));

            case ExpressionType.AndAssign: return(Expr.AndAssign(left, right, method, conversion));

            case ExpressionType.ArrayIndex: return(Expr.ArrayIndex(left, right));

            case ExpressionType.Assign: return(Expr.Assign(left, right));

            case ExpressionType.Coalesce: return(Expr.Coalesce(left, right, conversion));

            case ExpressionType.Divide: return(Expr.Divide(left, right, method));

            case ExpressionType.DivideAssign: return(Expr.DivideAssign(left, right, method, conversion));

            case ExpressionType.Equal: return(Expr.Equal(left, right, liftToNull, method));

            case ExpressionType.ExclusiveOr: return(Expr.ExclusiveOr(left, right, method));

            case ExpressionType.ExclusiveOrAssign: return(Expr.ExclusiveOrAssign(left, right, method, conversion));

            case ExpressionType.GreaterThan: return(Expr.GreaterThan(left, right, liftToNull, method));

            case ExpressionType.GreaterThanOrEqual: return(Expr.GreaterThanOrEqual(left, right, liftToNull, method));

            case ExpressionType.LeftShift: return(Expr.LeftShift(left, right, method));

            case ExpressionType.LeftShiftAssign: return(Expr.LeftShiftAssign(left, right, method, conversion));

            case ExpressionType.LessThan: return(Expr.LessThan(left, right, liftToNull, method));

            case ExpressionType.LessThanOrEqual: return(Expr.LessThanOrEqual(left, right, liftToNull, method));

            case ExpressionType.Modulo: return(Expr.Modulo(left, right, method));

            case ExpressionType.ModuloAssign: return(Expr.ModuloAssign(left, right, method, conversion));

            case ExpressionType.Multiply: return(Expr.Multiply(left, right, method));

            case ExpressionType.MultiplyAssign: return(Expr.MultiplyAssign(left, right, method, conversion));

            case ExpressionType.MultiplyAssignChecked: return(Expr.MultiplyAssignChecked(left, right, method, conversion));

            case ExpressionType.MultiplyChecked: return(Expr.MultiplyChecked(left, right, method));

            case ExpressionType.NotEqual: return(Expr.NotEqual(left, right, liftToNull, method));

            case ExpressionType.Or: return(Expr.Or(left, right, method));

            case ExpressionType.OrAssign: return(Expr.OrAssign(left, right, method, conversion));

            case ExpressionType.OrElse: return(Expr.OrElse(left, right, method));

            case ExpressionType.Power: return(Expr.Power(left, right, method));

            case ExpressionType.PowerAssign: return(Expr.PowerAssign(left, right, method, conversion));

            case ExpressionType.RightShift: return(Expr.RightShift(left, right, method));

            case ExpressionType.RightShiftAssign: return(Expr.RightShiftAssign(left, right, method, conversion));

            case ExpressionType.Subtract: return(Expr.Subtract(left, right, method));

            case ExpressionType.SubtractAssign: return(Expr.SubtractAssign(left, right, method, conversion));

            case ExpressionType.SubtractAssignChecked: return(Expr.SubtractAssignChecked(left, right, method, conversion));

            case ExpressionType.SubtractChecked: return(Expr.SubtractChecked(left, right, method));

            default: throw new NotSupportedException();
            }
        }
예제 #10
0
 public override SysExpr ToExpression() => SysExpr.ArrayIndex(Left.ToExpression(), Right.ToExpression());
예제 #11
0
 public Expr ToExpression() => Expr.ArrayIndex(Array.ToExpression(), Index.ToExpression());
예제 #12
0
 public static Expression GetByIndex(this Expression expression, params Expression[] indexer)
 => Expression.ArrayIndex(expression, indexer);
        public LambdaExpression CreateLambda(Type from, Type to)
        {
            var input          = Ex.Parameter(from, "input");
            var eType          = to.GetElementType();
            var res            = Ex.Parameter(typeof(ConversionResult <>).MakeGenericType(eType).MakeArrayType(), "res");
            var end            = Ex.Label(typeof(ConversionResult <>).MakeGenericType(to), "end");
            var fromParameters = from.GetTypeInfo().GenericTypeArguments;
            var converters     = fromParameters.Select(t => new { Lambda = Ref.GetLambda(t, eType), Input = t }).ToArray();

            var block = Ex.Block(new[] { res },
                                 Ex.Assign(res, Ex.NewArrayBounds(typeof(ConversionResult <>).MakeGenericType(eType), Ex.Constant(fromParameters.Length))),
                                 Ex.Block(converters.Select((con, i) =>
                                                            Ex.Block(
                                                                Ex.Assign(Ex.ArrayAccess(res, Ex.Constant(i)), con.Lambda.ApplyTo(Ex.PropertyOrField(input, $"Item{i + 1}"))),
                                                                Ex.IfThen(Ex.Not(Ex.Property(Ex.ArrayIndex(res, Ex.Constant(i)), nameof(IConversionResult.IsSuccessful))),
                                                                          Ex.Goto(end, NoResult(to)))))),
                                 Ex.Label(end, Result(to,
                                                      Ex.NewArrayInit(eType,
                                                                      Enumerable.Range(0, fromParameters.Length)
                                                                      .Select(idx => Ex.Property(Ex.ArrayIndex(res, Ex.Constant(idx)), nameof(IConversionResult.Result)))))));
            var lambda = Ex.Lambda(block, input);

            return(lambda);
        }