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?18 Replies
If it helps, here's DefineAPI called on the DrawSprite line:
And called on the subsequent line with DrawLODSprite:
ok then what happens if you assign DrawSprite to an Action<...>
do you have overloads
I get an exception that I can't convert it -
The parameters are identical
this seems weird
i would still try to get signature of method via reflection
Not quite sure what you mean or what that would do?
like api.GetType().GetMethod("DrawSprite")
Ahh, will do
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>))...
Same deal
Is there something specific you wanted to check by getting DrawSprite from relfection?
wh
It works if I do this
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?
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
but the other method also has default params and he says it works
Then there must be another detail that hasn't been shared
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