gpt4 book ai didi

java - 将 boolean 类添加到类数组中

转载 作者:行者123 更新时间:2023-12-02 04:50:21 25 4
gpt4 key购买 nike

我有一些用 Java 编写的代码,我想将其转换为 Xamarin (C#)。我怎样才能用 C# 编写这个?

private static final Class<?>[] mSetForegroundSignature = new Class[] { boolean.class };
private static final Class<?>[] mStartForegroundSignature = new Class[] { int.class, Notification.class };
private static final Class<?>[] mStopForegroundSignature = new Class[] { boolean.class };

我不知道如何获取这个“boolean.class”,并且“ ”也不起作用。因为这样就会被调用

Method mStartForeground = getClass().getMethod("startForeground", mStartForegroundSignature);

然后为每个喜欢一些包装

if (mStartForeground != null) {
mStartForegroundArgs[0] = Integer.valueOf(id);
mStartForegroundArgs[1] = notification;
invokeMethod(mStartForeground, mStartForegroundArgs);
return;
}

最佳答案

相当于 Class<T> 的 C#是 Type ,以及 boolean.class这是typeof(bool) - 如果您想了解有关 C# 反射的更多信息,请查看 this tutorial 。这是翻译后的代码:

  • 签名:

    private static readonly Type[] mSetForegroundSignature = new Type[] { typeof(bool) };
    private static readonly Type[] mStartForegroundSignature = new Type[] { typeof(int), typeof(Notification) };
    private static readonly Type[] mStopForegroundSignature = new Type[] { typeof(bool) };

  • 获取方法对象:

    System.Reflection.MethodInfo mStartForeground = new this.GetType().GetMethod("startForeground", mStartForegroundSignature);

  • 调用方法:

    if(mStartForeground != null) {
    mStartForegroundArgs[0] = Convert.ToInt32(id);
    mStartForegroundArgs[1] = notification;
    //invoke via reflection (it may be different to invokeMethod?)
    mStartForeground.Invoke(instance, mStartForegroundArgs);
    return;
    }
  • 关于java - 将 boolean 类添加到类数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29299019/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com