2014-11-08 32 views
0

我想創建一個類,從用戶獲取一個類的名稱和未知數量的參數,並創建一個合適的對象。 當我運行我的代碼,我得到一個MissingMethod異常。從c中的參數數量未知的字符串創建一個對象#

這是我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Project2 
{ 
    class Class1 
    { 
     public static void Main() 
     { 
      Console.WriteLine("enter a class name"); 
      string className = Console.ReadLine(); 
      Console.WriteLine("enter parameters seperate with a space"); 
      string parametersLine = Console.ReadLine(); 
      string []parameters = parametersLine.Split(' '); 
      //Console.WriteLine(parameters.Length); 
      CreateObject createObject = new CreateObject(className, parameters); 
      Console.ReadKey(); 
     } 
    } 

    class CreateObject 
    { 
    private readonly string className; 
     public CreateObject(string className, string[] parameters) 
     { 
      this.className = className; 
      try 
      { 
       if (parameters.Length == 1 && parameters[0] == "") // no parameters 
       { 
        Activator.CreateInstance(className.GetType()); 
       } 

       else { 
        Activator.CreateInstance(className.GetType(), parameters); 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.GetType()); 
      } 
     } 
    } 

    class Person 
    { 
     public int id{ get; set; } 
     public string name{ get; set; } 
     public Person() 
     { 
      Console.WriteLine("Created empety Person object"); 
     } 
     public Person(int id, string name) 
     { 
      this.id = id; 
      this.name = name; 
      Console.WriteLine("Created Person object. " +"id:" + id + " name:" + name); 
     } 
    } 

    class Point 
    { 
     public int x { get; set; } 
     public int y { get; set; } 
     public int z { get; set; } 

     public Point() 
     { 
      Console.WriteLine("Created empety Point object"); 
     } 
     public Point(int x, int y, int z) 
     { 
      this.x = x; 
      this.y = y; 
      this.z = z; 
      Console.WriteLine("Created Point object. " + "x:" + x + " y:" + y + " z:" + z); 
     } 
    } 
} 
+2

您的代碼只能找到將* string *作爲參數類型的構造函數。必須將輸入的參數值轉換爲正確的類型將是非常重要的。很難猜出爲什麼你需要解決這個問題。 – 2014-11-08 18:19:28

+0

只在開始。它與沒有參數的構造函數一起工作。 – 2014-11-08 18:49:20

回答

2

你正在創造String類的實例,這個類沒有,你是調用構造函數。如果你能得到參數的類型,這個代碼將起作用。記住使用類的全名(例如的CreateObject( 「Project2.Point」,新的對象[] {1,1,1}))

public CreateObject(string className, object[] parameters) 
    { 
     this.className = className; 
     try 
     { 
      if (parameters.Length == 1 && parameters[0] == "") // no parameters 
      { 
       Activator.CreateInstance(Type.GetType(className)); 
      } 

      else 
      { 
       Activator.CreateInstance(Type.GetType(className), parameters); 
      } 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.GetType()); 
     } 
    } 

編輯:

可以簡化這樣的代碼:

public CreateObject(string className, object[] parameters) 
    { 
     this.className = className; 
     try 
     { 
      var insntance = Activator.CreateInstance(Type.GetType(className), parameters); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.GetType()); 
     } 
    } 

我嘗試了以下的例子並正常工作:

CreateObject createObject1 = new CreateObject("Project2.Point", new object[0]); 
CreateObject createObject2 = new CreateObject("Project2.Point", new object[] { 1, 2, 3 }); 
CreateObject createObject3 = new CreateObject("Project2.Person", new object[0]); 
CreateObject createObject4 = new CreateObject("Project2.Person", new object[] { 1, "name"}); 

enter image description here

+0

我修復它像你寫的,但是當我運行它與一個空的Con',我得到一個ArgumentNullException。 – 2014-11-09 03:31:03

+0

我認爲你在閱讀參數時做錯了什麼。我編輯了我的初始答案,以便看到一些例子。 – 2014-11-09 12:06:31

相關問題