public static TRes Cast <TRes>(object obj) { Type reqType = typeof(TRes); if (IsCLRInstance(obj)) { throw new JNIException("Can't cast CLR instance of " + obj.GetType() + " to " + reqType); } IJvmProxy proxy = obj as IJvmProxy; if (!reqType.IsInterface && !typeof(IObject).IsAssignableFrom(reqType)) { throw new JNIException("Can't cast to CLR class"); } RegistryRecord record = Registry.GetCLRRecord(reqType); Class clazz = proxy.getClass(); if (!record.JVMInterface.isAssignableFrom(clazz)) { throw new InvalidCastException("Can't cast JVM instance of " + clazz + " to " + record.JVMInterface + "\n (" + clazz.getClassLoader() + "->" + record.JVMInterface.getClassLoader() + ")"); } IJvmProxy res = record.CopyCLRProxy(JNIEnv.ThreadEnv, proxy.JvmHandle); return((TRes)res); }
public static TRes CreateProxy <TRes>(JniGlobalHandle jvmHandle) { Type reqType = typeof(TRes); if (!reqType.IsInterface && !typeof(IObject).IsAssignableFrom(reqType)) { throw new JNIException("Can't create proxy to CLR class"); } RegistryRecord record = Registry.GetCLRRecord(reqType); JNIEnv env = JNIEnv.ThreadEnv; Class clazz = env.GetObjectClass(jvmHandle); if (!record.JVMInterface.isAssignableFrom(clazz)) { throw new InvalidCastException("Can't cast JVM instance of " + clazz + " to " + record.JVMInterface + "\n (" + clazz.getClassLoader() + "->" + record.JVMInterface.getClassLoader() + ")"); } IJvmProxy res = record.CopyCLRProxy(env, jvmHandle); return((TRes)res); }