0

My code is .Net 4.0 and I am trying to understand some legacy code I now work with. I can't change it at the moment and i'm sure this code has worked before my time. It needs to make an enum from strings, but the type is not recognized as an enum.


EDIT

I now realize the enum property is actually nullable. So it is a NetType? How can I convert that into an enum if it has a value?

When I debug and see the type that is being checked on the enum, this is what I see:

FullName = System.Nullable1[[AppName.Model.NetType, AppName.Model, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]
Name = Nullable
1

This is the enum:

public enum NetType
{
    ChoiceOne = 1,
    ChoiceTwo = 2
}

Main code, simplified for clarity:

var property = typeof(MainClass).GetProperty("NetType");
var value = GetValue(property.PropertyType, "ChoiceOne");

private object GetValue(Type type, string valueString)
{
     if (type.IsEnum)// Why false?
         return Enum.Parse(type, valueString);

     if (type == typeof (Int32))
         return Int32.Parse(valueString);

     return valueString;
 }
Impurity
  • 1,037
  • 2
  • 16
  • 31
Daarwin
  • 2,896
  • 7
  • 39
  • 69
  • 2
    Looks to me like the type is `NetType?` which is not an enum, but a struct containing one – BradleyDotNET Aug 22 '18 at 14:47
  • Oh snap! Thats true :o So how can i properly identify that as an enum and parse it? – Daarwin Aug 22 '18 at 14:48
  • what about using enum.IsDefined() – PiJei Aug 22 '18 at 14:49
  • 1
    You'll need to check if `type` is a `Nullable` and then check whether it's generic type argument `T`is an enum. This [question](https://stackoverflow.com/questions/2723048/checking-if-type-instance-is-a-nullable-enum-in-c-sharp) has the specifics to get you started. – Kirk Larkin Aug 22 '18 at 14:51
  • Why do we not see the definition of `MainClass` in this question? – Kenneth K. Aug 22 '18 at 14:53
  • Possible duplicate of [Checking if Type instance is a nullable enum in C#](https://stackoverflow.com/questions/2723048/checking-if-type-instance-is-a-nullable-enum-in-c-sharp) – Jeroen Mostert Aug 22 '18 at 15:06
  • @KirkLarkin that did the trick. Thank you! – Daarwin Aug 22 '18 at 15:10

2 Answers2

0

Try this:

var type = property.PropertyType;
object value;
if (type.IsEnum)
{
    value = GetValue(type, "ChoiceOne");
}
else if (type.IsGenericType &&
    type.GetGenericTypeDefinition() == typeof(Nullable<>) &&
    type.GetGenericArguments()[0].IsEnum)
{
    value = GetValue(type.GetGenericArguments()[0], "ChoiceOne");
}

It should work for both NetType and NetType?.

mm8
  • 163,881
  • 10
  • 57
  • 88
0

Another option is to use Nullable.GetUnderlyingType() to check to see if it's a nullable enum first. If it is, use the underlying enum; if it's not, do your checks as you normally did.

private object GetValue(Type type, string valueString)
{
    // If Nullable.GetUnderlyingType() returns null, it's not nullable
    // and you can default to type.
    var enumCandidiate = Nullable.GetUnderlyingType(type) ?? type;

    if (enumCandidiate.IsEnum)
        return Enum.Parse(enumCandidiate, valueString);

     if (type == typeof (Int32))
         return Int32.Parse(valueString);

     return valueString;
}

Here was my test script (using RoslynPad, hence the Dump() calls).

var val = GetValue(typeof(NetType), "ChoiceOne");
val.Dump();

val = GetValue(typeof(NetType?), "ChoiceOne");
val.Dump();
Ari Roth
  • 5,392
  • 2
  • 31
  • 46