Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Extension methods cannot be dynamically dispatched ...

92 views
Skip to first unread message

Shapper

unread,
Mar 24, 2012, 6:31:48 AM3/24/12
to
Hello,

On a class library I have the following extension:

public static Int32 Execute(this IDbConnection cnn, String sql, dynamic param = null)

I am trying to expose this extension through a method on my class:

public class Repository : IRepository {

private IDbConnection _connection;

public Repository(IDbConnection connection) {
_connection = connection;
} // Repository

public Int32 Execute(String sql, dynamic param = null) {
return _connection.Execute(sql, param);
} // Execute

} // Repository

But I keep getting the following error:

'System.Data.IDbConnection' has no applicable method named 'Execute' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

I tried to change my code in a few ways but no luck ...

Any idea of how to solve this?

Thank You,
Miguel

Peter Duniho

unread,
Mar 24, 2012, 12:16:28 PM3/24/12
to
On Sat, 24 Mar 2012 03:31:48 -0700 (PDT), Shapper wrote:

> [...]
> But I keep getting the following error:
>
> 'System.Data.IDbConnection' has no applicable method named 'Execute' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
>
> I tried to change my code in a few ways but no luck ...
>
> Any idea of how to solve this?

Yes. Do what the error message tells you to do: "Consider casting the
dynamic arguments or calling the extension method without the extension
method syntax."

Given what little code you've posted so far, likely the latter will be the
most fruitful approach in your scenario.

Arne Vajhøj

unread,
Mar 24, 2012, 1:26:55 PM3/24/12
to
The error message has two suggestions.

But I think you should go a third route.

I am highly skeptical about you really needing dynamic, so I
would suggest you try whether object or maybe object[] / List<object>
would work just as well for you.

Arne


Shapper

unread,
Mar 25, 2012, 10:12:35 AM3/25/12
to
Yes, in fact for a 3.0 version there is object[] ...

But I was able so solve it. I just need to use something like:

SqlMapper.Execute( ... )

I forgot that ...

Thank You,
Miguel

Peter Duniho

unread,
Mar 25, 2012, 12:05:26 PM3/25/12
to
On Sun, 25 Mar 2012 07:12:35 -0700 (PDT), Shapper wrote:

> [...]
> But I was able so solve it. I just need to use something like:
>
> SqlMapper.Execute( ... )

Just as the error message told you to do.

> I forgot that ...

Humans forget. That's why it's useful to read the error messages. They
contain reminders.

Arne Vajhøj

unread,
Mar 25, 2012, 8:59:43 PM3/25/12
to
On 3/25/2012 10:12 AM, Shapper wrote:
> On Saturday, March 24, 2012 5:26:55 PM UTC, Arne Vajhøj wrote:
>> On 3/24/2012 6:31 AM, Shapper wrote:
>>> On a class library I have the following extension:
>>>
>>> public static Int32 Execute(this IDbConnection cnn, String sql, dynamic param = null)
>>>
>>> I am trying to expose this extension through a method on my class:
>>>
>>> public class Repository : IRepository {
>>>
>>> private IDbConnection _connection;
>>>
>>> public Repository(IDbConnection connection) {
>>> _connection = connection;
>>> } // Repository
>>>
>>> public Int32 Execute(String sql, dynamic param = null) {
>>> return _connection.Execute(sql, param);
>>> } // Execute
>>>
>>> } // Repository
>>>
>>> But I keep getting the following error:
>>>
>>> 'System.Data.IDbConnection' has no applicable method named 'Execute' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
>>>
>>> I tried to change my code in a few ways but no luck ...
>>>
>>> Any idea of how to solve this?
>>
>> The error message has two suggestions.
>>
>> But I think you should go a third route.
>>
>> I am highly skeptical about you really needing dynamic, so I
>> would suggest you try whether object or maybe object[] / List<object>
>> would work just as well for you.
>
> Yes, in fact for a 3.0 version there is object[] ...
>
> But I was able so solve it. I just need to use something like:
>
> SqlMapper.Execute( ... )
>
> I forgot that ...

If SqlMapper is the class of your extension method then that is
one of the suggestions in the error message.

But even though you now have gotten it to compile, then
I still can not see any reason for using dynamic.

Arne

0 new messages