要测试一个方法的返回类型是否为 System.Void
,您可以使用反射来获取方法的返回类型,并将其与 System.Void
类型进行比较。以下是一个示例代码:
using System;
using System.Reflection;
public class Test
{
public void MyMethod()
{
// ...
}
}
public class Program
{
public static void Main()
{
Type type = typeof(Test);
MethodInfo methodInfo = type.GetMethod("MyMethod");
if (methodInfo.ReturnType == typeof(void))
{
Console.WriteLine("The method returns System.Void.");
}
else
{
Console.WriteLine("The method does not return System.Void.");
}
}
}
在这个示例中,我们首先获取了 Test
类的 MyMethod
方法的 MethodInfo
对象。然后,我们将 MethodInfo.ReturnType
属性与 typeof(void)
进行比较,如果它们相等,则表示方法的返回类型为 System.Void
。
领取专属 10元无门槛券
手把手带您无忧上云