-2

Let's say i have this class:

class Person
{
   public string Name { get; set; }
   public HairColour hairColour { get; set; }
   public EyeColour eyeColour { get; set; }
}

where HairColour and EyeColour are enums.

And I want the user to enter a value for each of the person's properties like this:

string userInput = "";

Person p = new Person();

Type personType = typeof(Person);

foreach (var property in personType.GetProperties())
{
   Console.Write("Enter the person's " + property.Name + ": ")
   userInput = Console.ReadLine();
   personType.GetProperty(property.Name).SetValue(p, // PARSE PROPERTY HERE );
}

Is there a way to cast the userInput to the corresponding type, without using multiple "if" statements?

k0ntrol
  • 17
  • 4
  • 1
    Does this answer your question? [Convert a string to an enum in C#](https://stackoverflow.com/questions/16100/convert-a-string-to-an-enum-in-c-sharp) – Michael Welch Oct 31 '20 at 21:44
  • as `SetValue` expects an `object`, you don´t need the actual compile-time type. Just use `Enum.Parse` and pass ot to `SetValue`. – MakePeaceGreatAgain Oct 31 '20 at 21:44
  • I think question is about how to know which type to convert to "without if .. else statements"? – Fabio Oct 31 '20 at 21:45
  • Unless you are building very very generic solution for multiple classes, I would suggest to have dedicated "input" class which will ask for values for a particular class with "hardcoded" properties and their types – Fabio Oct 31 '20 at 21:47
  • Aside that this is not a good use case for reflection, if you want to keep on this direction you should at least always make some assertion that the convertion from string (user input) and the type (enum, int, or whatever property you will end up using in your ```Person``` class, is possible, as the user could write virtually any string in the input. Check this methods here maybe you will find them usefull, but bare in mind that this is by no means advisable https://stackoverflow.com/questions/2961656/generic-tryparse – Rod Ramírez Oct 31 '20 at 21:49

2 Answers2

0

You're looking for Enum.Parse.

0

as SetValue expects an object, you don´t need the actual compile-time type. Just use Enum.Parse and pass ot to SetValue.

userInput = Console.ReadLine();
personType.GetProperty(property.Name).SetValue(p, Enum.Parse(property.PropertyType, userInput));

or even better Enum.TryParse, as your input may contain invalid characters as well.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • How in the loop of properties "I know" that I need to use `Enum.Parse`? – Fabio Oct 31 '20 at 21:47
  • `property.PropertyType.IsEnum` is how you could tell – pinkfloydx33 Oct 31 '20 at 21:56
  • OP asked how to determine which "parser" to use based on property information (string vs enum vs int), not how to parse an enum and **without if .. else statements** – Fabio Oct 31 '20 at 21:56
  • @Fabio You are correct,my question is: how do I know when to use the Enum.Parse without checking if the property's type is a HairColour or an EyeColour – k0ntrol Oct 31 '20 at 22:00
  • @k0ntrol you don´t need to know the exact type - at least not at compile-time - just that it is an `enum`. Only at **runtime** you need the type - which you get via `property.PropertyType`. – MakePeaceGreatAgain Nov 01 '20 at 15:03