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 Funcs 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?
Func<>? Why not simply create your own delegate to pass the method around? Something likepublic delegate MyStruct MyFunc(in MyStruct input). --Func<>delegates are basically just that and they are "prepared for your convenience".inwas added in C# 7.2.Func<>(atm?), because there is noFunc<>defined like that. See referencesource. For one or two parameters, the framework could provideinvariants. But it get's pretty tedious pretty fast with more parameters (could be automated, but then you'd have billions of delegates in intellisense...).