Skip to content

Request to Contribute: Replace Unsafe.As with Safer Type Checks #29

@hasanxdev

Description

@hasanxdev

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:

  1. Safer and more readable
  2. Compatible with common DI scenarios and collection types
  3. 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions