コード例 #1
0
        private ConstantValue2 GetCSharpConstantValueHack(ICSharpExpression expression, IType type)
        {
            if (expression == null)
                return ConstantValue2.NOT_COMPILE_TIME_CONSTANT;

            IArrayCreationExpression arrayExpression = expression as IArrayCreationExpression;
            if (arrayExpression != null)
            {
                int[] dimensions = arrayExpression.Dimensions;
                int rank = dimensions.Length;
                for (int i = 0; i < rank; i++)
                    if (dimensions[i] != 1)
                        return ConstantValue2.NOT_COMPILE_TIME_CONSTANT;

                IArrayType arrayType = arrayExpression.Type() as IArrayType;
                if (arrayType == null)
                    return ConstantValue2.NOT_COMPILE_TIME_CONSTANT;

                IArrayInitializer arrayInitializer = arrayExpression.Initializer;
                if (arrayInitializer == null)
                    return ConstantValue2.NOT_COMPILE_TIME_CONSTANT;
                
                IType elementType = arrayType.ElementType;

                Type resolvedScalarType = MakeType(arrayType.GetScalarType()).Resolve(true);
                if (resolvedScalarType == typeof(Type))
                    resolvedScalarType = typeof(IType);

                Type resolvedElementType = rank == 1 ? resolvedScalarType : rank == 2 ? resolvedScalarType.MakeArrayType() : resolvedScalarType.MakeArrayType(rank - 1);

                IList<IVariableInitializer> elementInitializers = arrayInitializer.ElementInitializers;
                int length = elementInitializers.Count;
                Array array = Array.CreateInstance(resolvedElementType, length);

                for (int i = 0; i < length; i++)
                {
                    IExpressionInitializer initializer = elementInitializers[i] as IExpressionInitializer;
                    if (initializer == null)
                        return ConstantValue2.NOT_COMPILE_TIME_CONSTANT;

                    ConstantValue2 elementValue = GetCSharpConstantValueHack(initializer.Value, elementType);
                    if (elementValue.IsBadValue())
                        return ConstantValue2.NOT_COMPILE_TIME_CONSTANT;

                    array.SetValue(elementValue.Value, i);
                }

                return new ConstantValue2(array, arrayType, type.GetManager().Solution);
            }

            ITypeofExpression typeExpression = expression as ITypeofExpression;
            if (typeExpression != null)
            {
                return new ConstantValue2(typeExpression.ArgumentType, type, type.GetManager().Solution);
            }

            return expression.ConstantCalculator.ToTypeImplicit(expression.CompileTimeConstantValue(), type);
        }