What causes “extension methods cannot be dynamically dispatched” here?

自闭症网瘾萝莉.ら 提交于 2019-11-27 09:23:56

So, can somebody please help me understand why leveraging the same overload inside of those other methods is failing with that error?

Precisely because you're using a dynamic value (param) as one of the arguments. That means it will use dynamic dispatch... but dynamic dispatch isn't supported for extension methods.

The solution is simple though: just call the static method directly:

return SqlMapper.Query(_connection, sql, param, transaction,
                       buffered, commandTimeout, commandType);

(That's assuming you really need param to be of type dynamic, of course... as noted in comments, you may well be fine to just change it to object.)

Another solution to the same issue is to apply type casting to the dynamic value.

I encountered the same compile error with:

Url.Asset( "path/" + article.logo );

Which was resolved by doing:

Url.Asset( "path/" + (string) article.logo );

Note: the dynamic value is well-known to be a string, in this case; a fact reinforced by the string concatenation that is present.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!