Search code examples
c#portable-class-library

How to check if a type is a struct from a portable library


Traditionally I would test if a type is a struct as such:

public void Do<TType>(TType value)
{  
   if (typeof(TType).IsValueType) 
   {
      // code for structs
   }
   else
   {
      // code for non structs
   }
}

But now I find the IsValueType property is not available in the portable class library with the frameworks I'm targeting. How would one go about to check if it is a struct or not then?

I'm targeting:

  • .NET Framework 4.5
  • ASP.NET Core 5
  • Windows 8
  • Windows Phone 8.1
  • Xamarin.Android
  • Xamarin.iOS
  • Xamarin.iOS (Classic)

Solution

  • Try

    using System.Reflection;
    
    typeof(TType).GetTypeInfo().IsValueType