1

Reflection code.

I can check if myTypeObject == typeof(decimal) || myTypeObject == typeof(decimal?)

Is there any way to do that without repeating decimal?

I'm guessing something along the lines of:

myRealTypeObject = myTypeObject.IsNullable() ? myTypeObject.GetTypeInsideNullability() : myTypeObject;
myRealTypeObject == typeof(decimal)
Brondahl
  • 7,402
  • 5
  • 45
  • 74

1 Answers1

1

You can use Nullable.GetUnderlyingType which returns null if the input type is not nullable:

var myRealTypeObject = Nullable.GetUnderlyingType(myTypeObject) ?? myTypeObject;

if instead you have have some object you want to check you can just use is (or as):

bool isDecimal = boxedDecimal is decimal?;
Lee
  • 142,018
  • 20
  • 234
  • 287
  • it's specifically reflection code (EF type conventions, if you care), but the 1st half is exactly what I was hoping for. – Brondahl Nov 24 '18 at 17:44