I'm trying to declare a Func<T, TResult>
for a static method on a struct that has in
parameters.
The signature of my method is something like this:
public static MyStruct MyMethod(in MyStruct input);
I tried to declare the Func
this way:
new Func<MyStruct, MyStruct>(MyStruct.MyMethod);
But this causes an error:
"No overload for 'MyMethod' matches delegate
Func<MyStruct, MyStruct>
."
I did some googling, and found the following information in the Microsoft docs:
The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have one parameter that is passed to it by value, and that it must return a value. (https://learn.microsoft.com/en-us/dotnet/api/system.func-2?view=netcore-3.1)
From my code, I can see this to be true as I have some Func
s working properly for methods on this struct with in
parameters, and they have another argument passed by value.
So my question has two parts, firstly why does at least one argument to a Func<>
have to be passed by value? And secondly, how can I achieve getting a Func<>
which will encapsulate the static method on my struct?