简体   繁体   中英

How to cast C# interface to Java.Lang.Object?

Currently, I am working on IEGL10 in Xamarin. I have implemented ISurfaceHolderCallback and on SurfaceCreated(ISurfaceHolder holder) I have to call a method like this.

 public void SurfaceCreated(ISurfaceHolder holder)
 {
   mEglSurface = mEgl.EglCreateWindowSurface(mEglDisplay, mEglConfig,
            holder, null);
 }

The problem is, the holder is a C# interface and EglCreateWindowSurface requires Java.Lang.Object. So how can I do the casting. If I directly cast holder like (Java.Lang.Object)holder . It is throwing invalid cast exception. Please help guys I am really stuck here.

How to cast C# interface to Java.Lang.Object?

MonoDroid has integrated extension for this purpose :

Java.Lang.Object holder_object = holder.JavaCast<Java.Lang.Object>(); 

EGLSurface mEglSurface = mEgl.EglCreateWindowSurface(mEglDisplay, mEglConfig, holder_object, null);

You could see the document :

public static class Extensions
{
    //
    // Summary:
    //     /// Performs an Android runtime-checked type conversion. ///
    //
    // Parameters:
    //   instance:
    //     /// An Android.Runtime.IJavaObject instance to convert /// to a TResult instance.
    //     ///
    //
    // Type parameters:
    //   TResult:
    //     /// The type to convert instance to. /// TResult must implement the /// Android.Runtime.IJavaObject
    //     interface. ///
    //
    // Returns:
    //     /// A TResult representation for /// instance. ///
    //
    // Exceptions:
    //   T:System.ArgumentException:
    //     ///
    //     /// The JNI class for TResult cannot be found. ///
    //     ///
    //     -or-
    //     ///
    //     /// The proxy class for TResult is /// abstract, and the non-abstract Proxy can't
    //     be found. ///
    //     ///
    //
    //   T:System.InvalidCastException:
    //     /// The Anrdroid object instance instance.Handle /// cannot be converted to the
    //     Android type corresponding to /// TResult. ///
    //
    //   T:System.NotSupportedException:
    //     /// An unknown error occurred. ///
    //
    // Remarks:
    //     /// /// This is a hack, but a currently necessary one. /// ///
    //     /// Most of the Android types are staticly generated /// wrappers over a description
    //     of the underlying Android types. This /// intermediate description does not expose
    //     implementation details, /// which sometimes must be relied upon. ///
    //     ///
    //     /// For example, consider the /// Javax.Microedition.Khronos.Egl.EGLContext.EGL
    //     /// property, which returns an instance of the /// Javax.Microedition.Khronos.Egl.IEGL
    //     /// interface. This interface is useless, containing no members to /// invoke
    //     or use. The developer is instead expected to convert this /// instance to an
    //     interface which contains actual operations, such as /// the Javax.Microedition.Khronos.Egl.IEGL10
    //     interface. /// Unfortunately, the MonoDroid-generated wrappers do not know this,
    //     /// nor can they (the EGL10 implementation may be removed in a /// future Android
    //     version). The result is that if developers attempt /// to cast within managed
    //     code, the result will be a /// System.InvalidCastException: ///
    //     /// EGL10 egl10 = (EGL10) EGLContext.EGL; // throws ///
    //     /// The JavaCast() method allows performing such type conversions /// while bypassing
    //     the managed type system and instead relying upon /// the Android runtime system
    //     to perform the type checking. This /// allows: ///
    //     /// EGL10 egl10 = EGLContext.EGL.JavaCast<EGL10>(); // good ///
    public static TResult JavaCast<TResult>(this IJavaObject instance) where TResult : class, IJavaObject;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM