[RFC] Better support for protocols when wrapping WeakDelegates.
- Dominant language
- C#
- Stars
- 2.9k
- Forks
- 576
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
Reference: #3885
For this api definition:
```csharp
[NullAllowed, Export ("delegate", ArgumentSemantic.Weak)]
NSObject WeakDelegate { get; set; }
[Wrap ("WeakDelegate")]
[NullAllowed]
ICLSDataStoreDelegate Delegate { get; set; }
```
we generate:
```csharp
[CompilerGenerated]
public ICLSDataStoreDelegate Delegate {
get {
return WeakDelegate as ICLSDataStoreDelegate;
}
set {
var rvalue = value as NSObject;
if (value != null && rvalue == null)
throw new ArgumentException ("The object passed of type " + value.GetType () + " does not derive from NSObject");
WeakDelegate = rvalue;
}
}
```
Problems:
1. The `Delegate` getter only works if `WeakDelegate` returns the a type that implements the expected protocol. If it returns a type we don't know about (or a type that doesn't formally implement the expected protocol), then the cast check will fail and the getter will return null.
2. The `Delegate` setter only works if the value is an `NSObject`. It's possible it might not be (our protocol wrapper types don't inherit from `NSObject` for instance).
Proposed solution:
```csharp
public ICLSDataStoreDelegate Delegate {
get {
ICLSDataStoreDelegate ret;
if (IsDirectBinding) {
ret = Runtime.GetINativeObject (global::ObjCRuntime.Messaging.IntPtr_objc_msgSend (this.Handle, Selector.GetHandle ("delegate")), false);
} else {
ret = Runtime.GetINativeObject (global::ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper (this.SuperHandle, Selector.GetHandle ("delegate")), false);
}
return ret;
}
set {
if (IsDirectBinding) {
global::ObjCRuntime.Messaging.void_objc_msgSend_IntPtr (this.Handle, Selector.GetHandle ("setDelegate:"), value.GetHandle ());
} else {
global::ObjCRuntime.Messaging.void_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("setDelegate:"), value.GetHandle ());
}
}
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.