Extension methods cannot be dynamically dispatched ...

Discussion in 'Microsoft C# .NET' started by Shapper, Mar 24, 2012.

  1. Shapper

    Shapper Guest

    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 bedynamically 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
     
    Shapper, Mar 24, 2012
    #1
    1. Advertisements

  2. Shapper

    Arne Vajhøj Guest

    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.

    Arne
     
    Arne Vajhøj, Mar 24, 2012
    #2
    1. Advertisements

  3. Shapper

    Shapper Guest

    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.
    >
    > Arne


    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
     
    Shapper, Mar 25, 2012
    #3
  4. Shapper

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Mar 26, 2012
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    C# Language Specification - Extension Methods

    Guest, Sep 20, 2005, in forum: Microsoft C# .NET
    Replies:
    4
    Views:
    246
    Guest
    Sep 20, 2005
  2. Martin Carpella
    Replies:
    17
    Views:
    311
    Jon Skeet [C# MVP]
    May 10, 2006
  3. Steeve

    Anonymus methods in anonymus methods performance

    Steeve, Sep 20, 2006, in forum: Microsoft C# .NET
    Replies:
    3
    Views:
    174
    Guest
    Sep 21, 2006
  4. koredump
    Replies:
    3
    Views:
    240
    Ben Voigt [C++ MVP]
    Jul 19, 2007
  5. Varangian
    Replies:
    2
    Views:
    333
    Jeremy Shovan
    May 15, 2008
Loading...

Share This Page