C
C#2mo 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
SamPersonOP2mo ago
If it helps, here's DefineAPI called on the DrawSprite line:
No description
SamPerson
SamPersonOP2mo ago
And called on the subsequent line with DrawLODSprite:
No description
Omnissiah
Omnissiah2mo ago
ok then what happens if you assign DrawSprite to an Action<...> do you have overloads
SamPerson
SamPersonOP2mo 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;
Omnissiah
Omnissiah2mo ago
this seems weird i would still try to get signature of method via reflection
SamPerson
SamPersonOP2mo ago
Not quite sure what you mean or what that would do?
Omnissiah
Omnissiah2mo ago
like api.GetType().GetMethod("DrawSprite")
SamPerson
SamPersonOP2mo ago
Ahh, will do
SamPerson
SamPersonOP2mo 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
SamPersonOP2mo ago
No description
SamPerson
SamPersonOP2mo ago
Same deal Is there something specific you wanted to check by getting DrawSprite from relfection? wh
SamPerson
SamPersonOP2mo ago
It works if I do this
No description
SamPerson
SamPersonOP2mo 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
333fred2mo 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
Omnissiah
Omnissiah2mo ago
but the other method also has default params and he says it works
333fred
333fred2mo ago
Then there must be another detail that hasn't been shared
MODiX
MODiX2mo 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
333fred2mo 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