In C# (Xamarin), “int.class” can be expressed as “typeof(int)”.

huangapple go评论64阅读模式
英文:

how to express `int.class` in c#(xamarin)?

问题

在Java中,我们可以这样写:

Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);

但在C#(Xamarin)中编写时:

var method = extraNotification.Class.GetDeclaredMethod("setMessageCount", Java.Lang.Class.ForName("java.lang.Integer"));

会抛出 {Java.Lang.NoSuchMethodException} 异常。

似乎 Java.Lang.Class.ForName("java.lang.Integer") 不等同于 int.class,那么如何表示后者呢?

英文:

we can write below in java:

Method method = extraNotification.getClass().getDeclaredMethod(“setMessageCount”, int.class);

but when write in c#(xamarin):

var method = extraNotification.Class.GetDeclaredMethod("setMessageCount", Class.ForName("java.lang.Integer"));

it will throw {Java.Lang.NoSuchMethodException}

seems that Class.ForName("java.lang.Integer") is not equal int.class, so how to express the later?

答案1

得分: 1

感谢 @Selvin,

Java 中的 int.class 可以通过以下方式获取:

var ic = Class.forName("java.lang.Integer").getField("TYPE").get(null) as Class;
var method = extraNotification.getClass().getDeclaredMethod("setMessageCount", ic);
英文:

thanks to @Selvin,

the java int.class can be retrieved as below:

var ic = Class.ForName("java.lang.Integer").GetField("TYPE").Get(null) as Class;
var method = extraNotification.Class.GetDeclaredMethod("setMessageCount", ic );

huangapple
  • 本文由 发表于 2020年1月3日 22:05:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定