I want to create an instance of a class using the name which the user has provided in the code.
I know about the Activator.CreateInstance
method which has been the answer to similar questions but without an example I'm stumped.
class Program
{
static void Main()
{
Program Pet = new Program();
string Name = Pet.GetName();
Pet.GetSpecies(Name);
}
public void GetSpecies(string name)
{
Console.WriteLine($"What species is {name}? We currently see: Dog/Cat/Hamster/Gerbil");
string answer = Console.ReadLine();
answer = answer.ToLower();
if (answer == "dog")
{
Console.WriteLine("You have selected: Dog");
Dog name = new Dog(); //?
}
}
public string GetName()
{
Console.WriteLine("And what is your pets name?");
string name = Console.ReadLine();
return name;
}
}
Aucun commentaire:
Enregistrer un commentaire