[generator] Remove temporary creation of `NSArray` instances in generated bindings
- Dominant language
- C#
- Stars
- 2.9k
- Forks
- 576
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
Look for them using
```bash
$ git grep NSArray src/generat*.cs
```
There's a few example that generates them. Here's one from `Trampolines.g.cs`
## Current Code
```csharp
unsafe void Invoke (nint statusFlag1, nint statusFlag2, global::CoreNFC.EncryptionId encryptionIdentifier, NSData[] nodeKeyVersionListAes, NSData[] nodeKeyVersionListDes, NSError error)
{
if (nodeKeyVersionListAes is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (nodeKeyVersionListAes));
if (nodeKeyVersionListDes is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (nodeKeyVersionListDes));
var error__handle__ = error.GetHandle ();
var nsa_nodeKeyVersionListAes = nodeKeyVersionListAes is null ? null : NSArray.FromNSObjects (nodeKeyVersionListAes);
var nsa_nodeKeyVersionListDes = nodeKeyVersionListDes is null ? null : NSArray.FromNSObjects (nodeKeyVersionListDes);
invoker (BlockPointer, statusFlag1, statusFlag2, (nint) (Int64) encryptionIdentifier, nsa_nodeKeyVersionListAes is null ? IntPtr.Zero : nsa_nodeKeyVersionListAes.Handle, nsa_nodeKeyVersionListDes is null ? IntPtr.Zero : nsa_nodeKeyVersionListDes.Handle, error__handle__);
if (nsa_nodeKeyVersionListAes != null)
nsa_nodeKeyVersionListAes.Dispose ();
if (nsa_nodeKeyVersionListDes != null)
nsa_nodeKeyVersionListDes.Dispose ();
}
```
## Better Code
```csharp
unsafe void Invoke (nint statusFlag1, nint statusFlag2, global::CoreNFC.EncryptionId encryptionIdentifier, NSData[] nodeKeyVersionListAes, NSData[] nodeKeyVersionListDes, NSError error)
{
if (nodeKeyVersionListAes is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (nodeKeyVersionListAes));
if (nodeKeyVersionListDes is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (nodeKeyVersionListDes));
var error__handle__ = error.GetHandle ();
var nodeKeyVersionListAes__handle__ = CFArray.Create (nodeKeyVersionListAes);
var nodeKeyVersionListDes__handle__ = CFArray.Create (nodeKeyVersionListDes);
invoker (BlockPointer, statusFlag1, statusFlag2, (nint) (Int64) encryptionIdentifier, nodeKeyVersionListAes__handle__, nodeKeyVersionListDes__handle__, error__handle__);
CFObject.ReleaseNative (nodeKeyVersionListAes__handle__);
CFObject.ReleaseNative (nodeKeyVersionListDes__handle__);
}
```
## Changes
* remove duplicate null checks - it's done on the argument (non null) and before creating the `NSArray`
* remove temporary managed `NSArray` creation - it's only created to get an `Handle`
## Notes
The same pattern also exists in manual bindings - but there's more "bang in a buck" to fix it first inside the generator.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.