我有一些用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;
}
答案 0 :(得分:1)
在C#中,它被称为Type而不是Class。事情的名称有点不同:
private static readonly Type[] mSetForegroundSignature = new Type[] { typeof(bool) };
答案 1 :(得分:1)
与Class<T>
等效的c#为Type
,boolean.class
为typeof(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;
}