c# - Xamarin.Android - typeof 和 GetType 始终返回 System.RuntimeType

标签 c# android xamarin reflection types

我正在开发一个 Xamarin.Android WebView 应用程序,并尝试使用 Reflection 来实现一些 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 的其他人是否遇到过这种情况?如果是这样,有解决方法吗?它使我无法使用 Reflection,我需要它在 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

关于c# - Xamarin.Android - typeof 和 GetType 始终返回 System.RuntimeType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48914836/

相关文章:

c# - 使用 TimeSpan 作为定时器间隔

android - 在 Qt Android 中在哪里进行最后一分钟的清理?

android - 即使在 Android Wear 中使用前台服务和部分唤醒锁定,传感器数据也会在屏幕关闭时暂停

android - InstantiationException:无法实例化 fragment 确保类名存在,是公共(public)的,并且有一个空的公共(public)构造函数

android - Xamarin.Forms 项目中已添加 NuGet 包

c# - 如何填充类中包含类的列表

c# - 在一个解决方案中使用 C# 和 VB.NET

c# - 我可以隐藏 Xamarin.Auth NavigationBar 吗?

android - 无法解析 : com. google.firebase :firebase-core:11. 0.0

xamarin - 我可以使用 Mac Mini 和 VS 2015 进行跨平台开发吗?