【发布时间】:2021-04-17 20:14:51
【问题描述】:
我正在尝试使用 by-ref 参数作为参数在 Action 类型上创建委托。但我被困在如何实现这一点上。
这就是我目前得到的:
using System;
class Program
{
struct MyStruct { }
static void FooFn(in MyStruct s) { }
delegate void Foo (in MyStruct s);
public static void Main(string[] args)
{
Foo f = FooFn;
var t1 = typeof(Action<>).MakeGenericType(typeof(MyStruct));
var t2 = typeof(Action<>).MakeGenericType(typeof(MyStruct).MakeByRefType());
var d = Delegate.CreateDelegate(t1, null, f.Method);
}
}
在委托中使用“t1”的问题是签名不匹配。这是意料之中的,因为我通过引用传递结构。
t2 也不起作用,因为它抱怨我不能使用 By-ref 参数作为参数列表类型。 :(
在我的示例中,有没有办法使用 to Foo 创建委托?还是我必须删除 in 修饰符?
【问题讨论】:
标签: c# in-parameters