簡體   English   中英

Xamarin.Android - typeof 和 GetType 始終返回 System.RuntimeType

[英]Xamarin.Android - typeof and GetType always return System.RuntimeType

我正在開發一個 Xamarin.Android WebView 應用程序,並嘗試使用反射來實現一些 AJAX 方法,我注意到Object.GetTypetypeof總是返回System.RuntimeType

無論我傳遞給他們什么,都會發生這種情況 - 當我將stringintMyClass等傳遞給 typeof 以及在這些類的實例上調用 GetType 時,都會發生這種情況。

最小可運行代碼是微不足道的:

private class HybridWebViewClient : WebViewClient
{
    public static type Test() {
        Log.Debug("TypeDebug", typeof(string));
        Log.Debug("TypeDebug", typeof(bool));
        Log.Debug("TypeDebug", typeof(MyClass));
        Log.Debug("TypeDebug", "test".GetType());
        Log.Debug("TypeDebug", (34).GetType());
        Log.Debug("TypeDebug", (new MyClass()).GetType());
    }
}

以上所有打印“System.RuntimeType”。 使用 .NET 編譯的等效代碼會打印預期值 - System.String、System.Bool 等。

有沒有其他人使用 Xamarin.Android 遇到過這個問題? 如果是這樣,是否有解決方法? 它使我無法使用反射,我需要它在 WebView 中運行的 JavaScript 和應用程序本身之間傳遞信息

Android.Util.Log.Debug/Info/...需要一個string作為第二個參數,所以我不確定你的代碼是如何編譯的...

您應該輸出該類型的字符串表示形式:

Log.Debug("TypeDebug", typeof(string).FullName);
Log.Debug("TypeDebug", typeof(bool).FullName);
Log.Debug("TypeDebug", "test".GetType().FullName);
Log.Debug("TypeDebug", (34).GetType().FullName);

或者

Log.Debug("TypeDebug", $"{typeof(string)}");
Log.Debug("TypeDebug", $"{typeof(bool)}");
Log.Debug("TypeDebug", $"{"test".GetType()}");
Log.Debug("TypeDebug", $"{(34).GetType()}");
輸出:
 02-21 20:38:29.101 4243 4243 D TypeDebug: System.String 02-21 20:38:29.101 4243 4243 D TypeDebug: System.Boolean 02-21 20:38:29.101 4243 4243 D TypeDebug: System.String 02-21 20:38:29.101 4243 4243 D TypeDebug: System.Int32

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM