Ejemplo n.º 1
0
        public override void ExitExpArray([NotNull] SBP.ExpArrayContext context)
        {
            var valueParserExpressions = m_expressionData.PopStackLevel().ToList();

            for (int i = 0; i < valueParserExpressions.Count; i++)
            {
                valueParserExpressions[i] = this.ResolveForGetOperation(valueParserExpressions[i]);
            }

            bool first = true, same = true;
            Type firstElementType = null;

            foreach (var t in valueParserExpressions.Select(exp => exp.DataType.Type))
            {
                if (first)
                {
                    firstElementType = t; first = false;
                }
                else
                {
                    if (t != firstElementType)
                    {
                        same = false;
                        break;
                    }
                }
            }
            if (same)
            {
                var arrayValueExpressions = Expression.NewArrayInit(firstElementType, valueParserExpressions.Select(exp => exp.ExpressionCode).Reverse().ToArray());

                if (firstElementType.IsArray || firstElementType.IsGenericType)
                {
                    if (firstElementType.IsGenericType)
                    {
                        if (firstElementType.IsArray)
                        {
                            throw new NotImplementedException("Element type is array.");
                        }
                        else
                        {
                            throw new NotImplementedException("");
                        }
                    }
                    else
                    {
                        throw new NotImplementedException("");
                    }
                }
                else
                {
                    var    typeGeneric = typeof(List <>);
                    Type[] typeArgs    = { firstElementType };
                    var    listType    = typeGeneric.MakeGenericType(typeArgs);
                    var    ctor        = listType.GetConstructor(new Type[] { firstElementType.MakeArrayType() });
                    var    listCtor    = Expression.New(ctor, arrayValueExpressions);
                    m_expressionData.Push(new SBExpressionData(listCtor));

                    // http://stackoverflow.com/questions/1151464/how-to-dynamically-create-generic-c-sharp-object-using-reflection
                }
            }
            else
            {
                throw new NotImplementedException("Elements in array are not the same type.");
            }
        }
Ejemplo n.º 2
0
 public override void EnterExpArray([NotNull] SBP.ExpArrayContext context)
 {
     m_expressionData.PushStackLevel("ExpressionArray");
 }