我使用以下方法在comboBox中插入类中方法名称的列表。
Type t = typeof(Functions);
// public instance methods
MethodInfo[] methods = t.GetMethods(BindingFlags.Static | BindingFlags.Public);
var methodsNames = methods.Select(i => i.Name).ToArray();
// add them to combobox
comboBox1.Items.AddRange(methodsNames);
我希望它只包含返回特定类型的那些(例如int)。
如果我有这些方法:
public static int CountSameSizes(BinTreeNode<string> BT)
public static string CountLeaves(BinTreeNode<string> BT)
public static string CountAtLeastOneLeaf(BinTreeNode<string> BT)
我希望它只返回第一个。
答案 0 :(得分:2)
t.GetMethods(BindingFlags.Static | BindingFlags.Public).Cast<MethodInfo>()
.Where(method => method.ReturnType == typeof(int))
答案 1 :(得分:0)
试试这个:
public IEnumerable<string> GetMethodsOfReturnType(Type cls, Type ret)
{
var methods = cls.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
var retMethods = methods.Where(m => m.ReturnType.IsSubclassOf(ret) || m.ReturnType == ret)
.Select(m => m.Name);
return retMethods;
}