简体   繁体   English

如何将C#接口转换为Java.Lang.Object?

[英]How to cast C# interface to Java.Lang.Object?

Currently, I am working on IEGL10 in Xamarin. 目前,我正在Xamarin上开发IEGL10。 I have implemented ISurfaceHolderCallback and on SurfaceCreated(ISurfaceHolder holder) I have to call a method like this. 我已经实现了ISurfaceHolderCallback并且在SurfaceCreated(ISurfaceHolder holder)我必须调用这样的方法。

 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. 问题是,持有人是C#接口,EglCreateWindowSurface需要Java.Lang.Object。 So how can I do the casting. 那我该如何铸造。 If I directly cast holder like (Java.Lang.Object)holder . 如果我直接转换(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? 如何将C#接口转换为Java.Lang.Object?

MonoDroid has integrated extension for this purpose : MonoDroid为此目的集成了扩展:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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