我正在使用一个第三方库,它有一个事件\事件处理程序,我想将其转换为
系统ArgumentException-无法绑定到目标方法,因为其签名与委托类型的签名不兼容.
从…起
System.Private.CoreLib.dll!System.Reflection.RuntimeMethodInfo.CreateDelegateInternal(System.Type delegateType, object firstArgument, System.DelegateBindingFlags bindingFlags) Line 556 C#
System.Private.CoreLib.dll!System.Reflection.RuntimeMethodInfo.CreateDelegate(System.Type delegateType, object target) Line 534 C#
System.Reactive.dll!System.Reactive.ReflectionUtils.CreateDelegate<Test.TheDelegate>(object o, System.Reflection.MethodInfo method) Line 18 C#
If I remove the in 从…起 the arguments, It works as expected. I'm at a loss to understand why? Is even possible to do?
using System;
using System.Diagnostics;
using System.Reactive.Linq;
Test test = new();
Start();
test.TriggerEvent();
test.TriggerEvent();
void Start()
{
Debug.WriteLine("Start");
var o = Observable.FromEvent<Test.TheDelegate, SomeStruct>
(h => test.TheEvent += h,
h => test.TheEvent -= h)
.Subscribe(OnNext);
}
void OnNext(SomeStruct s) => Debug.WriteLine("OnNext");
public class Test
{
public delegate void TheDelegate(in SomeStruct o);
// Without the in (or ref) keyword, no exception
//public delegate void TheDelegate(SomeStruct o);
public event TheDelegate? TheEvent;
public void TriggerEvent()
{
SomeStruct s = new();
TheEvent?.Invoke(s);
Debug.WriteLine("Event Fired");
}
}
public struct SomeStruct
{ }