I've been trying to enumerate through a properties of a class and getting their values in a List of strings. But i faced a problem where i get a NullReferenceException when the value is null from the properties
I Managed to Fix that by that solution, but still i don't see that as a clean code.
I Wonder if this can be implemented in a cleaner and more professional way.
private int CalculateScore()
{
var score = 0;
var answers = new List<string>();
foreach (var prop in typeof(TypesSheet).GetProperties())
{
// WHEN STRING IS MISSING IT BREAKS
try
{
var answer = prop.GetValue(_typesSheet).ToString();
answers.Add(answer);
}
catch
{
continue;
}
}
if (_gameMode == GameMode.SinglePlayer)
{
foreach (var answer in answers)
{
if (string.IsNullOrEmpty(answer))
continue;
score = score + 10;
}
return score;
}
}