2009-04-29 100 views

回答

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; 
     } 
    } 
} 
+1

Tnx很多。在這些答案之間,只有這對我有效...... – 2012-12-21 20:33:42

+0

如果沒有命名空間,該怎麼辦?我們如何提供typeName – MonsterMMORPG 2016-03-01 10:45:32

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); 

    } 
相關問題