23
A
回答
44
你只是想调用一个无参数的构造函数来创建实例吗?该类型是否也被指定为字符串,或者是否可以将其作为通用方法?例如:
// All error checking omitted. In particular, check the results
// of Type.GetType, and make sure you call it with a fully qualified
// type name, including the assembly if it's not in mscorlib or
// the current assembly. The method has to be a public instance
// method with no parameters. (Use BindingFlags with GetMethod
// to change this.)
public void Invoke(string typeName, string methodName)
{
Type type = Type.GetType(typeName);
object instance = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod(methodName);
method.Invoke(instance, null);
}
或
public void Invoke<T>(string methodName) where T : new()
{
T instance = new T();
MethodInfo method = typeof(T).GetMethod(methodName);
method.Invoke(instance, null);
}
7
假设你想调用的方法不带任何参数:
public void InvokeMethod(Type type, string methodName)
{
object instance = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
method.Invoke(instance, null);
}
3
我觉得你的问题是有点太普通了这里,我我在这里提供了一些假设的解决方案。
假设:您有一个typeName(字符串),methodName(字符串)和一个参数(SomeType)。
public static void InvokeMethod(string typeName, string methodName, SomeType objSomeType) {
Type type = Type.GetType(typeName);
if(type==null) {
return;
}
object instance = Activator.CreateInstance(type); //Type must have a parameter-less contructor, or no contructor.
MethodInfo methodInfo =type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public);
if(methodInfo==null) {
return;
}
methodInfo.Invoke(instance, new[] { objSomeType });
}
让我知道我的假设是否错误。
16
要调用一个构造函数,Activator.CreateInstance将会诀窍。它有一堆重载让你的生活更轻松。
如果你的构造函数是parameterless:
object instance = Activator.CreateInstance(type)
如果您需要parameters:
object instance = Activator.CreateInstance(type, param1, param2)
要调用的方法,一旦你有Type对象,你可以调用GetMethod
得到method,然后Invoke
(带或不带参数)来调用它。如果你需要它,Invoke也会给你所调用函数的返回值(如果它是一个无效方法,则返回null),
对于稍微更详细的示例(粘贴到控制台应用程序中去):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
namespace Test
{
public static class Invoker
{
public static object CreateAndInvoke(string typeName, object[] constructorArgs, string methodName, object[] methodArgs)
{
Type type = Type.GetType(typeName);
object instance = Activator.CreateInstance(type, constructorArgs);
MethodInfo method = type.GetMethod(methodName);
return method.Invoke(instance, methodArgs);
}
}
class Program
{
static void Main(string[] args)
{
// Default constructor, void method
Invoker.CreateAndInvoke("Test.Tester", null, "TestMethod", null);
// Constructor that takes a parameter
Invoker.CreateAndInvoke("Test.Tester", new[] { "constructorParam" }, "TestMethodUsingValueFromConstructorAndArgs", new object[] { "moo", false });
// Constructor that takes a parameter, invokes a method with a return value
string result = (string)Invoker.CreateAndInvoke("Test.Tester", new object[] { "constructorValue" }, "GetContstructorValue", null);
Console.WriteLine("Expect [constructorValue], got:" + result);
Console.ReadKey(true);
}
}
public class Tester
{
public string _testField;
public Tester()
{
}
public Tester(string arg)
{
_testField = arg;
}
public void TestMethod()
{
Console.WriteLine("Called TestMethod");
}
public void TestMethodWithArg(string arg)
{
Console.WriteLine("Called TestMethodWithArg: " + arg);
}
public void TestMethodUsingValueFromConstructorAndArgs(string arg, bool arg2)
{
Console.WriteLine("Called TestMethodUsingValueFromConstructorAndArg " + arg + " " + arg2 + " " + _testField);
}
public string GetContstructorValue()
{
return _testField;
}
}
}
2
要传递的参数动态地 在这里,我已经采取PARAMS字串[] args,因为不同的功能有不同的数目的参数左右。
public void Invoke(string typeName,string functionName,params string[] args)
{
Type type = Type.GetType(typeName);
dynamic c=Activator.CreateInstance(type);
//args contains the parameters(only string type)
type.InvokeMember(functionName,BindingFlags.InvokeMethod,null,c,args);
}
相关问题
- 1. 通过字符串名称的JavaScript函数调用
- 2. 获取调用的函数名称作为字符串
- 3. typeof调用函数的名称作为字符串传递
- 4. 从f的名称作为字符串调用函数#
- 5. 使用Clojure中函数名称的字符串调用函数
- 6. 函数名称作为字符串传递时调用嵌套函数
- 7. 通过字符串调用函数?
- 8. 使用字符串值作为函数的名称被称为
- 9. 基于包含函数名称的字符串调用函数
- 10. 如何通过将其名称作为字符串来使用函数
- 11. 将函数调用字符串解析为名称和参数
- 12. 在Awk中,如何通过使用字符串名称来调用函数?
- 13. 如何通过setInterval/setTimeout函数中的名称调用函数?
- 14. P /调用函数通过mangled名称
- 15. 通过名称调用函数
- 16. 通过字符串触发绑定“函数”与调用函数
- 17. 根据名称动态调用函数
- 18. 以任何方式将函数作为字符串调用?
- 19. 如何使用R中函数名称的字符串调用函数?
- 20. 如何在Python中获取函数名称作为字符串?
- 21. 如何获取函数的名称作为字符串?
- 22. Dart通过函数名称调用成员函数
- 23. 使用输入字符串作为函数名称C++
- 24. 使用JavaScript字符串作为函数名称?
- 25. 使用字符串作为函数名称 - golang
- 26. 作为函数名称调用常量?
- 27. 如何通过字符串中的JavaScript函数动态
- 28. 数组到字符串转换 - 变量函数名称调用
- 29. 从字符串名称动态创建一个JavaScript函数
- 30. 如何调用名称在字符串中定义的函数?
Tnx很多。在这些答案之间,只有这对我有效...... – 2012-12-21 20:33:42
如果没有命名空间,该怎么办?我们如何提供typeName – MonsterMMORPG 2016-03-01 10:45:32