-1

I wanna check if one type can be cast to another, for example

CanCast(typeof(int), typeof(float)) would return true however CanCast(typeof(bool), typeof(float)) would return false

Convert.ChangeType only works with a non-null value so inserting the types is not enough(some of them might be objects so their default value is null)

X is Type also only works with a value but that's not desirable

How would I be able to replicate the desired above?

1Mangomaster1
  • 346
  • 1
  • 6
  • 16
  • 2
    Why do you want this? What actual problem are you trying to solve with this? Perhaps what you really need is generics? You can use [Type.IsAssignableFrom](https://learn.microsoft.com/en-us/dotnet/api/system.type.isassignablefrom?view=net-7.0) and `IsAssignableTo` but that's relatively expensive and rarely needed. – Panagiotis Kanavos Aug 07 '23 at 17:07
  • Does this answer your question? [Test if Convert.ChangeType will work between two types](https://stackoverflow.com/questions/1399273/test-if-convert-changetype-will-work-between-two-types) – marsze Aug 07 '23 at 17:41

0 Answers0