예제 #1
0
        private static void LoadArg(ILGenerator il, int arg)
        {
            switch (arg)
            {
            case 0:
                il.Emit(OpCodes.Ldarg_0);
                break;

            case 1:
                il.Emit(OpCodes.Ldarg_1);
                break;

            case 2:
                il.Emit(OpCodes.Ldarg_2);
                break;

            case 3:
                il.Emit(OpCodes.Ldarg_3);
                break;

            default:
                il.Emit(OpCodes.Ldarg, arg);
                break;
            }
        }
예제 #2
0
        private static void Convert(ILGenerator il, Type from, Type to, MethodInfo fallbackConverter)   // TODO: add number conversion + array conversion
        {
            if (from == typeof(object))
            {
                if (to.IsValueType)
                {
                    il.Emit(OpCodes.Unbox_Any, to);
                }
                else
                {
                    il.Emit(OpCodes.Castclass, to);
                }

                return;
            }

            MethodInfo converter = getConversionOperator(from, to);

            if (converter == null && fallbackConverter != null)
            {
                converter = fallbackConverter.MakeGenericMethod(new Type[] { from, to });
            }

            if (converter == null)
            {
                throw new InvalidCastException($"Cannot cast from {from.Name} to {to.Name}, converter not found");
            }

            if (converter.IsGenericMethod)
            {
                try {
                    converter.Invoke(null, new object[] { null });
                } catch (InvalidCastException) {
                    throw new InvalidCastException($"Cannot cast from {from.Name} to {to.Name}");
                }
            }