Пример #1
0
        public static Object WrapCLR(object obj)
        {
            var proxy    = obj as IJvmProxy;
            var clrProxy = proxy as IClrProxy;

            if (proxy != null)
            {
                if (clrProxy != null)
                {
                    return((Object)clrProxy);
                }
                throw new JNIException("Can't wrap JVM instance");
            }
            Type   type = obj.GetType();
            JNIEnv env  = JNIEnv.ThreadEnv;

            RegistryRecord record   = Registry.GetCLRRecord(type);
            JniLocalHandle jvmProxy = record.CreateJVMProxy(env, obj);

            return((Object)__IClrProxy.CreateProxy(env, jvmProxy));
        }
Пример #2
0
        private static JniLocalHandle Cast(IntPtr @__envp, JniLocalHandle @__class, JniLocalHandle obj, JniLocalHandle expectedInterface)
        {
            JNIEnv env = JNIEnv.Wrap(@__envp);

            try
            {
                Class          expectedInterfaceClass = Convertor.StrongJ2CpClass(env, expectedInterface);
                object         real    = __IClrProxy.GetObject(env, obj);
                Type           type    = real.GetType();
                RegistryRecord record  = Registry.GetJVMRecord(expectedInterfaceClass);
                Type           reqType = record.CLRInterface;
                if (!reqType.IsAssignableFrom(type))
                {
                    throw new InvalidCastException("Can't convert instance of CLR type " + type + " to " + reqType);
                }

                return(record.CreateJVMProxy(env, real));
            }
            catch (Exception ex)
            {
                env.ThrowExisting(ex);
            }
            return(JniLocalHandle.Zero);
        }