-
-
Notifications
You must be signed in to change notification settings - Fork 27
Description
This is a contribution issue. If you're interested in helping out, please read on ❤️
Problem Description
The current version of the project uses Unsafe.As to cast objects to arrays. While this works in simple scenarios, it can lead to issues when the actual object is not really an array—for example, when using DI containers like AutoFac, or working with libraries that provide List or other types instead.
In such cases, using Unsafe.As can result in unexpected behavior or runtime exceptions.
Suggested Solution
Instead of relying on unsafe casting, it's better to explicitly check the type of the object using is pattern matching. Here's a suggested pattern:
if (objectA is SomeType[] array)
{
// Safe usage of the array
}
else if (objectA is List<SomeType> list)
{
// Convert list to array or handle accordingly
}
else
{
throw new NotSupportedException("Unsupported object type.");
}This approach is:
- Safer and more readable
- Compatible with common DI scenarios and collection types
- Prevents undefined or unstable behavior
How to Contribute
If you're interested in working on this:
Feel free to comment here to claim it, or open a pull request directly
Please also update or add related samples if possible
Thank you for your support and contributions!