2
votes

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)
1
Based on @romanoza answer I found this link giving a technical explanation as to why it function like this. visualstudiomagazine.com/articles/2012/11/29/…Cornelius

1 Answers

4
votes

Try

using System.Reflection;

typeof(TType).GetTypeInfo().IsValueType