C
C#5d ago
SamPerson

Named function magically turns into anonymous method for no clear reason, cannot be used as Action

I am storing a list of delegates, which are added via calls to DefineFunction(string name, Delegate actualFn). I have a method with signature public void DrawSprite(string sprite, float x, float y, float w, float h, float rot, float grayscale01 = 0). But when I call DefineFunction to add this method to the list, for some reason, DefineFunction takes it in not as a System.Action or System.Func derivative like every other call, but as an <>f__AnonymousDelegate0 type, which cannot later be converted to a System.Action for invocation. (as verified by calling actualFn.GetType() in DefineFunction) DrawSprite is the only method for which this happens. I have absolutely no clue why this is happening. It's exactly the same as everything else. It very much is a named function. Does anyone have ideas for things I could try to get to the bottom of this?
No description
No description
No description
18 Replies
SamPerson
SamPerson5d ago
If it helps, here's DefineAPI called on the DrawSprite line:
No description
SamPerson
SamPerson5d ago
And called on the subsequent line with DrawLODSprite:
No description
MutableString
MutableString5d ago
ok then what happens if you assign DrawSprite to an Action<...> do you have overloads
SamPerson
SamPerson5d ago
I get an exception that I can't convert it -
InvalidCastException: Unable to cast object of type

'<>f__AnonymousDelegate0`7[System.String,System.Single,System.Single,System.Single,System.Single,System.Single,System.Single]' to type
'System.Action`7 [System.String,System.Single,System.Single,System.Single,System.Single,System.Single,System.Single]'.
InvalidCastException: Unable to cast object of type

'<>f__AnonymousDelegate0`7[System.String,System.Single,System.Single,System.Single,System.Single,System.Single,System.Single]' to type
'System.Action`7 [System.String,System.Single,System.Single,System.Single,System.Single,System.Single,System.Single]'.
The parameters are identical
// exception
result.DrawSprite = (System.Action<System.String, System.Single, System.Single, System.Single, System.Single, System.Single, System.Single>)drawSpriteFn;

// no exception
result.DrawLODSprite = (System.Action<System.String, System.Single, System.Single, System.Single>)lodSpriteFn;
// exception
result.DrawSprite = (System.Action<System.String, System.Single, System.Single, System.Single, System.Single, System.Single, System.Single>)drawSpriteFn;

// no exception
result.DrawLODSprite = (System.Action<System.String, System.Single, System.Single, System.Single>)lodSpriteFn;
MutableString
MutableString5d ago
this seems weird i would still try to get signature of method via reflection
SamPerson
SamPerson5d ago
Not quite sure what you mean or what that would do?
MutableString
MutableString5d ago
like api.GetType().GetMethod("DrawSprite")
SamPerson
SamPerson5d ago
Ahh, will do
SamPerson
SamPerson5d ago
Shows exactly the same as api.DrawSprite, and if I pass it in with drawSpriteMethodInfo.CreateDelegate(typeof(System.Action<string, float, float, float, float, float, float>))...
No description
SamPerson
SamPerson5d ago
No description
SamPerson
SamPerson5d ago
Same deal Is there something specific you wanted to check by getting DrawSprite from relfection? wh
SamPerson
SamPerson5d ago
It works if I do this
No description
SamPerson
SamPerson5d ago
This gets recieved as a System.Action, it just adds a completley unnecessary level of indirection I feel mildly insane Is this a compiler bug or related to some intended behavior?
333fred
333fred5d ago
Default parameters are included in the signature And action does not have default parameters, so a custom delegate type is created If you want it to be action, cast it to action when you create it
MutableString
MutableString5d ago
but the other method also has default params and he says it works
333fred
333fred5d ago
Then there must be another detail that hasn't been shared
MODiX
MODiX5d ago
333fred
sharplab.io (click here)
ToDelegate(DrawSprite);
ToDelegate(DrawRect);
void DrawSprite(string s, float x, float y, float w, float...
void DrawRect(float x, float y, float h, float g = 0, floa...
void ToDelegate(Delegate d) => Console.WriteLine(d.GetType...
ToDelegate(DrawSprite);
ToDelegate(DrawRect);
void DrawSprite(string s, float x, float y, float w, float...
void DrawRect(float x, float y, float h, float g = 0, floa...
void ToDelegate(Delegate d) => Console.WriteLine(d.GetType...
React with ❌ to remove this embed.
333fred
333fred5d ago
A simple test app shows that both of these get anonymous delegate types And if you remove the default parameters, it'll instead print the Action types
Want results from more Discord servers?
Add your server