mercredi 14 octobre 2015

Behavior of Nullable.GetNullableType method

I have some C# code to detect if a type is nullable, and to get the underlying type if so:

...
    Log.DebugFormat("type is {0}", type.FullName);
    Type underlyingType = Nullable.GetUnderlyingType(type);
    if (underlyingType != null)
    {
        Log.DebugFormat("underlying type is {0}", underlyingType);
    }
    else
    {
        Log.DebugFormat("underlying type is null");
    }
...

Unfortunately, it does not seem to work. Here's some output if the type is a nullable int:

...
    type is System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]&
    underlying type is null
...

I have seen similar code snippets that seem to work, so am not sure why mine is not working. One thing I notice is that the type name has an & at the end - could this be significant?

Thanks for your help!





Aucun commentaire:

Enregistrer un commentaire