2014-11-08 3 views
0

클래스 이름과 알 수없는 매개 변수 수를 사용자로부터 가져 와서 적절한 객체를 만드는 클래스를 생성하려고합니다. 내 코드를 실행할 때 MissingMethod 예외가 발생합니다. 알 수없는 매개 변수 수를 가진 문자열에서 객체 생성하기 #

내 코드입니다 : 당신은 String 클래스의 인스턴스를 생성하고

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 *을 인수 유형으로 사용하는 생성자 만 찾을 수 있습니다. 입력 된 매개 변수 값을 올바른 유형으로 변환해야하는 것은 중요하지 않습니다. 이 문제를 해결해야하는 이유를 추측하기가 꽤 어렵습니다. –

+0

만이 처음입니다. 매개 변수가없는 생성자를 사용하여 작업 할 수도 있습니다. –

답변

2

이 클래스는 호출 된 생성자를 가지고 있지 않았다. 매개 변수의 유형을 얻을 수 있으면이 코드가 작동합니다. 클래스의 전체 이름을 사용해야합니다 (예 : 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()); 
     } 
    } 

편집 :

당신은 간단하게 할 수 이 같은 코드 :

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 :

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

나는 다음의 예와 제대로 작동 시도

+0

나는 당신을 비난 한 것처럼 고쳐 쓴다. 그러나 비어있는 Con와 함께 실행할 때, 나는 ArgumentNullException을 얻는다. –

+0

나는 매개 변수를 읽을 때 뭔가 잘못하고 있다고 생각합니다. 몇 가지 예를 볼 수 있도록 초기 응답을 편집했습니다. –

관련 문제