How would I check if a generic type is unmanaged, then pass it to a method with where T ; unmanaged?

I have a method that needs to call different methods based on whether T is managed or not. I'm not sure how to check if T is unmanaged, and also tell the compiler that it can use it in the method with the unmanaged constraint.
public unsafe T SomethingUnmanaged<T>(int stuff) where T : unmanaged
{
//do something
return whatever;
}
public unsafe T SomethingManaged<T>(int stuff)
{
//do something different
return whatever;
}
public unsafe T Something<T>(int stuff)
{
if (T is unmanaged)
return SomethingUnmanaged<T>(stuff);
else
return SomethingManaged<T>(stuff);
}
public unsafe T SomethingUnmanaged<T>(int stuff) where T : unmanaged
{
//do something
return whatever;
}
public unsafe T SomethingManaged<T>(int stuff)
{
//do something different
return whatever;
}
public unsafe T Something<T>(int stuff)
{
if (T is unmanaged)
return SomethingUnmanaged<T>(stuff);
else
return SomethingManaged<T>(stuff);
}
3 Replies
333fred
333fred3y ago
You could, in theory, use a ton of reflection to do it But you're probably boxing something that shouldn't be boxed if you do
NoSkillPureAndy
NoSkillPureAndyOP3y ago
I settled for a very big switch expression
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server