Reputation: 1076
I am trying to create a class that gets from the user a name of a class and unknown number of parameters, and creates a suitable object. when i run my code, i get a MissingMethod Exception.
this is my code:
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);
}
}
}
Upvotes: 1
Views: 1212
Reputation: 1316
You were creating instances of String class and this class did not have the constructor that you were calling. If you can get the types of the parameters this code will work. Remember to use the full name of the class (for example CreateObject("Project2.Point", new object[]{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());
}
}
EDIT:
You can simplify the code like this:
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());
}
}
I tried the following examples and working properly:
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"});
Upvotes: 2