使用类名和方法名调用类的成员

我试图使用Reflection调用类的函数(假设对象初始化不依赖于要调用的函数),就像这样

 ///  /// Calls a static public method. /// Assumes that the method returns a string ///  /// name of the assembly containing the class in which the method lives. /// namespace of the class. /// name of the class in which the method lives. /// name of the method itself. /// parameter passed to the method. /// the string returned by the called method. /// public static string InvokeStringMethod5(string assemblyName, string namespaceName, string typeName, string methodName, string stringParam) { //This method was created incase Method has params with default values. If has params will return error and not find function //This method will choice and is the preffered method for invoking // Get the Type for the class Type calledType = Type.GetType(String.Format("{0}.{1},{2}", namespaceName, typeName, assemblyName)); String s = null; // Invoke the method itself. The string returned by the method winds up in s. // Note that stringParam is passed via the last parameter of InvokeMember, as an array of Objects. if (MethodHasParams(assemblyName, namespaceName, typeName, methodName)) { s = (String)calledType.InvokeMember( methodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new Object[] { stringParam }); } else { s = (String)calledType.InvokeMember( methodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, null); } // Return the string that was returned by the called method. return s; } public static bool MethodHasParams(string assemblyName, string namespaceName, string typeName, string methodName) { bool HasParams; string name = String.Format("{0}.{1},{2}", namespaceName, typeName, assemblyName); Type calledType = Type.GetType(name); MethodInfo methodInfo = calledType.GetMethod(methodName); ParameterInfo[] parameters = methodInfo.GetParameters(); if (parameters.Length > 0) { HasParams = true; } else { HasParams = false; } return HasParams; } 

这是从这里采取的forms。

还有其他/更好的方法吗?

这项活动可以成为一般性的。 就像使用Dynamic一样,可以在.Net 4.0中调用Non-Static methods ,以便返回类型可以是独立的。

我从来没有在实际场景中使用dynamic关键字(只读了一些例子)来对我来说仍然不了解

在这方面的任何帮助/方向将不胜感激谢谢

回答关于dynamic ; 不,这不适合这里。 当在编译时知道成员名称(或操作)时, dynamic是有用的,但是不能certificate存在 – 基本上是鸭子类型。 例如:

 dynamic foo = GetSomeRandomObject(); foo.ThisShouldExist("abc"); 

这类似的东西,但用途不同。 所以,是的,你留下了反思。 你在做什么是非常正确的。 我唯一可能改变的是获取MethodInfo ,并从那里开始工作 – 尽管如果你可以改变API来接受单个string assemblyQualifiedName那么它将更加方便和灵活。 但也许:

 public static string InvokeStringMethod5(string assemblyName, string namespaceName, string typeName, string methodName, string stringParam) { string assemblyQualifiedName = string.Format("{0}.{1},{2}", namespaceName, typeName, assemblyName); Type calledType = Type.GetType(assemblyQualifiedName); if(calledType == null) throw new InvalidOperationException( assemblyQualifiedName + " not found"); MethodInfo method = calledType.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static); switch (method.GetParameters().Length) { case 0: return (string)method.Invoke(null, null); case 1: return (string)method.Invoke(null, new object[] { stringParam }); default: throw new NotSupportedException(methodName + " must have 0 or 1 parameter only"); } } 

要回答有关如何将结果转换为generics返回类型的问题,该方法将类似于:

 public static T InvokeMethod(string assemblyName, string namespaceName, string typeName, string methodName, string stringParam) { // instead of String s = null; T methodResult = default(T); // instead of s = (String)calledType.InvokeMember(...) methodResult = (T)calledType.InvokeMember(...); // return s; return methodResult; }