将boolean类添加到类数组中

时间:2015-03-27 10:54:17

标签: java c# class xamarin

我有一些用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”和“&lt;?&gt;”也行不通。 因为这会被称为

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

然后为每个包装器添加一些

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

2 个答案:

答案 0 :(得分:1)

在C#中,它被称为Type而不是Class。事情的名称有点不同:

private static readonly Type[] mSetForegroundSignature = new Type[] { typeof(bool) }; 

答案 1 :(得分:1)

Class<T>等效的c#为Typeboolean.classtypeof(bool) - 如果您想了解有关c#反射的更多信息,请查看{{3 }}。这是翻译的代码:

  • 签名:

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