Beispiel #1
0
 public object Convert(AphidInteropMethodArg arg, Type[] genericArguments) =>
 arg.ImplicitConversionOperator != null?
 Convert(arg.ImplicitConversionOperator, arg.Argument) :
     arg.ExplicitConversionOperator != null?
     Convert(arg.ExplicitConversionOperator, arg.Argument) :
         arg.IsValueTypeEnumerable &&
         genericArguments.Length >= 1 &&
         !genericArguments[0].IsValueType ?
         BoxValueTypeEnumerable((IEnumerable)arg.Argument) :
         Convert(arg.TargetType, arg.Argument, genericArguments);
Beispiel #2
0
        public AphidConversionInfo(
            AphidInteropMethodArg interopArg,
            bool canConvert,
            Type[] genericArguments)
        {
            InteropArg       = interopArg;
            CanConvert       = canConvert;
            GenericArguments = genericArguments;

            InteropArg.IsValueTypeEnumerable =
                GenericArguments.Length == 1 &&
                GenericArguments[0].IsValueType &&
                InteropArg.TargetType.GetGenericTypeDefinition() == typeof(IEnumerable <>);
        }
Beispiel #3
0
        public AphidConversionInfo CanConvert(
            AphidInteropMethodArg interopArg,
            object val,
            Type target)
        {
            Type valType;

            if (val == null)
            {
                return(new AphidConversionInfo(
                           interopArg,
                           !target.IsPrimitive && !target.IsEnum,
                           new Type[0]));
            }
            else if ((valType = val.GetType()) == typeof(AphidFunction) &&
                     target.IsDerivedFrom(typeof(Delegate)))
            {
                var isGeneric = target.IsGenericType;

                if (isGeneric)
                {
                    var targetGenericArgs = target.GetGenericArguments();
                    //var valGenericArgs = valType.GetGenericArguments();

                    //if (targetGenericArgs.Length > 0 && valGenericArgs.Length == 0)
                    //{
                    for (var i = 0; i < targetGenericArgs.Length; i++)
                    {
                        if (targetGenericArgs[i].IsGenericParameter)
                        {
                            targetGenericArgs[i] = typeof(object);
                        }
                    }

                    return(new AphidConversionInfo(
                               interopArg,
                               true,
                               targetGenericArgs));

                    //}

                    throw Interpreter.CreateRuntimeException(
                              "Converting from generic delegate {0} to {1} not yet supported.",
                              valType,
                              target);
                }

                return(new AphidConversionInfo(interopArg, true, Array.Empty <Type>()));
            }
            else if (val is AphidInteropMember &&
                     target.IsDerivedFrom(typeof(Delegate)))
            {
                //var methods = Array.FindAll(
                //    m.Members,
                //    x => x.MemberType == MemberTypes.Method);



                //if (methods.Length == 0)
                //{
                //    var callable = Array.FindAll(
                //        m.Members,
                //        x => ((x.MemberType == MemberTypes.Field &&
                //            ((FieldInfo)x).FieldType.IsDerivedFrom<Delegate>()) ||
                //            x.MemberType == MemberTypes.Property &&
                //            ((PropertyInfo)x).PropertyType.IsDerivedFrom<Delegate>()));

                //    if (callable.Length == 0)
                //    {
                //        throw Interpreter.CreateRuntimeException(
                //            "Converting from interop member {0} to delegate {1} not supported.",
                //            valType,
                //            target);
                //    }
                //}

                var isGeneric = target.IsGenericType;

                if (isGeneric)
                {
                    var targetGenericArgs = target.GetGenericArguments();
                    //var valGenericArgs = valType.GetGenericArguments();

                    //if (targetGenericArgs.Length > 0 && valGenericArgs.Length == 0)
                    //{
                    for (var i = 0; i < targetGenericArgs.Length; i++)
                    {
                        if (targetGenericArgs[i].IsGenericParameter)
                        {
                            targetGenericArgs[i] = typeof(object);
                        }
                    }

                    return(new AphidConversionInfo(
                               interopArg,
                               true,
                               targetGenericArgs));

                    //}

                    throw Interpreter.CreateRuntimeException(
                              "Converting from generic delegate {0} to {1} not yet supported.",
                              valType,
                              target);
                }

                return(new AphidConversionInfo(interopArg, true, Array.Empty <Type>()));
            }
            else
            {
                var genericArguments = new List <Type>();

                var canConvert =
                    valType == target ? true :
                    interopArg.ImplicitConversionOperator != null ? true :
                    valType == typeof(decimal) ? CanConvertOrBoxDecimal((decimal)val, target) :
                    //valType == typeof(sbyte) ? CanConvertOrBoxSByte((decimal)val, target) :
                    //valType == typeof(short) ? CanConvertOrBoxShort((decimal)val, target) :
                    //valType == typeof(int) ? CanConvertOrBoxInt((decimal)val, target) :
                    //valType == typeof(long) ? CanConvertOrBoxLong((decimal)val, target) :
                    //valType == typeof(byte) ? CanConvertOrBoxByte((decimal)val, target) :
                    //valType == typeof(ushort) ? CanConvertOrBoxUShort((decimal)val, target) :
                    //valType == typeof(uint) ? CanConvertOrBoxUInt((decimal)val, target) :
                    //valType == typeof(ulong) ? CanConvertOrBoxULong((decimal)val, target) :
                    //valType == typeof(float) ? CanConvertOrBoxFloat((decimal)val, target) :
                    //valType == typeof(double) ? CanConvertOrBoxDouble((decimal)val, target) :
                    valType == typeof(string) &&
                    (target == typeof(char[]) ||
                     (target == typeof(char) && ((string)val).Length == 1)) ? true :
                    valType.IsDerivedFromOrImplements(target, genericArguments) ? true :
                    target.IsArray ? CanConvertArray(val, valType, target) :
                    interopArg.ExplicitConversionOperator != null ? true :
                    target == typeof(string) ? true :
                    false;

                return(new AphidConversionInfo(
                           interopArg,
                           canConvert,
                           genericArguments.ToArray()));
            }
        }