泛型方法我有一个类查找静态类
public static class MyClass
{
public static T MyMethod<T>(T item) where T : ISomeInterface<T>, new
{
return MyMethod(new[] { item}).First();
}
public static IEnumerable<T> MyMethod<T>(params T[] items) where T : ISomeInterface<T>, new
{
// for simplicity
return items.ToList();
}
}
和一帮更为复杂的过载。 现在我想用
public static IEnumerable MyMethod(string typeName, params object[] items)
{
var type = Type.GetType(typeName, true, true);
var paramTypes = new Type[] { type.MakeArrayType() };
var method = typeof(MyClass).GetMethod(
"MyMethod", BindingFlags.Public | BindingFlags.Static
| BindingFlags.IgnoreCase, null, paramTypes, null);
return method.Invoke(null, new object[] { items });
}
但method
扩展类(因为我想,如果从PowerShell来调用)始终为空。这是通过GetMethod()
获得我的特定方法的正确方法。
什么是'table'变量?你的意思是'typeName'吗? –
这些方法是实例方法还是静态方法? –
'不能在静态类中声明实例成员'。 –