I am trying to create a delegate on an Action<> type with a by-ref parameter as argument. But I am stuck on how to achieve this.
That's what I got so far:
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);
}
}
The issue with using "t1" in the delegate is, that the signature does not match. That is kindof expected, as I am passing the struct by reference.
t2 does not work either, as it complains that I can not use a By-ref parameter as argument list type. :(
Is there a way to create the delegate with to Foo in my example? Or do I have to drop the in-modifier?