internal static Action <IntPtr, object []> CreateDelegate(Type type, ConstructorInfo cinfo, Type [] parameter_types)
        {
            var handle = handlefld;

            if (typeof(Java.Lang.Throwable).IsAssignableFrom(type))
            {
                handle = Throwable_handle;
            }

            DynamicMethod method = new DynamicMethod(DynamicMethodNameCounter.GetUniqueName(), typeof(void), new Type [] { typeof(IntPtr), typeof(object []) }, typeof(DynamicMethodNameCounter), true);
            ILGenerator   il     = method.GetILGenerator();

            il.DeclareLocal(typeof(object));

            il.Emit(OpCodes.Ldtoken, type);
            il.Emit(OpCodes.Call, gettype);
            il.Emit(OpCodes.Call, newobject);
            il.Emit(OpCodes.Stloc_0);
            il.Emit(OpCodes.Ldloc_0);
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Stfld, handle);

            il.Emit(OpCodes.Ldloc_0);
            for (int i = 0; i < parameter_types.Length; i++)
            {
                il.Emit(OpCodes.Ldarg, 1);
                il.Emit(OpCodes.Ldc_I4, i);
                il.Emit(OpCodes.Ldelem_Ref);
            }
            il.Emit(OpCodes.Call, cinfo);

            il.Emit(OpCodes.Ret);

            return((Action <IntPtr, object[]>)method.CreateDelegate(typeof(Action <IntPtr, object []>)));
        }
        public static Delegate CreateDelegate(Delegate dlg)
        {
            if (dlg == null)
            {
                throw new ArgumentNullException();
            }
            if (dlg.Target != null)
            {
                throw new ArgumentException();
            }
            if (dlg.Method == null)
            {
                throw new ArgumentException();
            }

            get_runtime_types();

            var ret_type    = dlg.Method.ReturnType;
            var parameters  = dlg.Method.GetParameters();
            var param_types = new Type [parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                param_types [i] = parameters [i].ParameterType;
            }

            var dynamic = new DynamicMethod(DynamicMethodNameCounter.GetUniqueName(), ret_type, param_types, typeof(DynamicMethodNameCounter), true);
            var ig      = dynamic.GetILGenerator();

            LocalBuilder?retval = null;

            if (ret_type != typeof(void))
            {
                retval = ig.DeclareLocal(ret_type);
            }

            ig.Emit(OpCodes.Call, wait_for_bridge_processing_method !);

            var label = ig.BeginExceptionBlock();

            for (int i = 0; i < param_types.Length; i++)
            {
                ig.Emit(OpCodes.Ldarg, i);
            }
            ig.Emit(OpCodes.Call, dlg.Method);

            if (retval != null)
            {
                ig.Emit(OpCodes.Stloc, retval);
            }

            ig.Emit(OpCodes.Leave, label);

            bool filter = Debugger.IsAttached || !JNIEnv.PropagateExceptions;

            if (filter)
            {
                ig.BeginExceptFilterBlock();

                ig.Emit(OpCodes.Call, mono_unhandled_exception_method !);
                ig.Emit(OpCodes.Ldc_I4_1);
                ig.BeginCatchBlock(null !);
            }
            else
            {
                ig.BeginCatchBlock(typeof(Exception));
            }

            ig.Emit(OpCodes.Dup);
            ig.Emit(OpCodes.Call, exception_handler_method !);

            if (filter)
            {
                ig.Emit(OpCodes.Throw);
            }

            ig.EndExceptionBlock();

            if (retval != null)
            {
                ig.Emit(OpCodes.Ldloc, retval);
            }

            ig.Emit(OpCodes.Ret);

            return(dynamic.CreateDelegate(dlg.GetType()));
        }
Exemplo n.º 3
0
        public static Delegate CreateDelegate(Delegate dlg)
        {
            if (dlg == null)
            {
                throw new ArgumentNullException();
            }
            if (dlg.Target != null)
            {
                throw new ArgumentException();
            }
            if (dlg.Method == null)
            {
                throw new ArgumentException();
            }
            get_runtime_types();
            Type returnType = dlg.Method.ReturnType;

            ParameterInfo[] parameters     = dlg.Method.GetParameters();
            Type[]          parameterTypes = new Type[parameters.Length];
            for (int index = 0; index < parameters.Length; ++index)
            {
                parameterTypes[index] = parameters[index].ParameterType;
            }
            DynamicMethod dynamicMethod = new DynamicMethod(DynamicMethodNameCounter.GetUniqueName(), returnType, parameterTypes, typeof(DynamicMethodNameCounter), true);
            ILGenerator   ilGenerator   = dynamicMethod.GetILGenerator();
            LocalBuilder  local         = null;

            if (returnType != typeof(void))
            {
                local = ilGenerator.DeclareLocal(returnType);
            }
            ilGenerator.Emit(OpCodes.Call, wait_for_bridge_processing_method);
            Label label = ilGenerator.BeginExceptionBlock();

            for (int index = 0; index < parameterTypes.Length; ++index)
            {
                ilGenerator.Emit(OpCodes.Ldarg, index);
            }
            ilGenerator.Emit(OpCodes.Call, dlg.Method);
            if (local != null)
            {
                ilGenerator.Emit(OpCodes.Stloc, local);
            }
            ilGenerator.Emit(OpCodes.Leave, label);
            bool flag = Debugger.IsAttached;// || !JNIEnv.PropagateExceptions;

            if (flag)
            {
                ilGenerator.BeginExceptFilterBlock();
                ilGenerator.Emit(OpCodes.Call, mono_unhandled_exception_method);
                ilGenerator.Emit(OpCodes.Ldc_I4_1);
                ilGenerator.BeginCatchBlock(null);
            }
            else
            {
                ilGenerator.BeginCatchBlock(typeof(Exception));
            }

            ilGenerator.Emit(OpCodes.Dup);
            ilGenerator.Emit(OpCodes.Call, exception_handler_method);
            if (flag)
            {
                ilGenerator.Emit(OpCodes.Throw);
            }
            ilGenerator.EndExceptionBlock();
            if (local != null)
            {
                ilGenerator.Emit(OpCodes.Ldloc, local);
            }
            ilGenerator.Emit(OpCodes.Ret);
            return(dynamicMethod.CreateDelegate(dlg.GetType()));
        }