Пример #1
0
        static void PrepareLogicalBoolean(int index, Type type, ArgumentConversions conversions)
        {
            if (type == TypeProvider.BooleanType)
            {
                return;
            }
            else if (type.IsPrimitive && type == typeof(bool))
            {
                conversions.Append(index, new ParamConversion(index, TypeProvider.BooleanType.GetMethod(TypeUtils.ImplicitConversionName, TypeUtils.PublicStatic, null, new Type[1] {
                    type
                }, null)));
                return;
            }
            else if (type.GetInterface(ReflectionUtils.ConvertibleType, false) != null)
            {
                if (type.IsValueType)
                {
                    conversions.Append(index, new BoxConversion(index, type));
                }
                conversions.Append(index, new ParamConversion(index, ReflectionHelpers.ToBoolean));
                return;
            }
            var methods = type.GetMember(TypeUtils.ImplicitConversionName, System.Reflection.MemberTypes.Method, TypeUtils.PublicStatic);
            var types   = new Type[] { type };

            foreach (System.Reflection.MethodInfo method in methods)
            {
                if (method.MatchesArgumentTypes(types, conversions) && method.ReturnType == TypeProvider.BooleanType)
                {
                    return;
                }
            }
            throw new Exception(string.Concat("can't convert from ", type, " to type Boolean"));
        }
Пример #2
0
        /// <inheritdoc/>
        Expression IExpressionVisitor <Expression> .VisitArrayLiteral(ArrayListExpression node)
        {
            var type = node.ArrayType != null?node.ArrayType.ResolveType(Context) : TypeProvider.AnyType;

            node.Type        = typeof(Collections.List <>).MakeGenericType(type);
            node.ElementType = type;
            if (node.Arguments != null)
            {
                var types = node.Arguments.Map(arg => arg.Accept(this).Type);
                if (node.Constructor == null)
                {
                    var ctors = node.Type.GetConstructors(ReflectionUtils.PublicInstance);
                    var ctor  = ReflectionUtils.BindToMethod(ctors, types, out ArgumentConversions conversions);
                    if (ctor == null)
                    {
                        ExecutionException.ThrowMissingMethod(node.Type, "ctor", node);
                    }
                    node.Constructor         = ctor;
                    node.ArgumentConversions = conversions;
                }
            }
            else if (node.Constructor == null)
            {
                node.Constructor = node.Type.GetConstructor(ReflectionUtils.PublicInstance, null, new Type[0], null);
            }
            var items = node.Expressions;

            if (items.Count > 0)
            {
                var arrayConversions = new ArgumentConversions(items.Count);
                for (int index = 0; index < items.Count; index++)
                {
                    var expression = items[index].Accept(this);
                    if (!TypeUtils.AreReferenceAssignable(type, expression.Type) && expression.Type.TryImplicitConvert(type, out System.Reflection.MethodInfo implicitCall))
                    {
                        if (expression.Type.IsValueType &&
                            implicitCall.GetParameters()[0].ParameterType == TypeProvider.ObjectType)
                        {
                            arrayConversions.Append(index, new BoxConversion(index, expression.Type));
                        }
                        arrayConversions.Append(index, new ParamConversion(index, implicitCall));
                    }
                }
                node.ArrayConversions = arrayConversions;
            }
            return(node);
        }