編譯錯誤
「System.Data.SqlClient.SqlConnection」有一個名爲「查詢」不適用的方法,但似乎有這個名字的擴展方法。擴展方法不能動態分派。考慮轉換動態參數或調用擴展方法而不使用擴展方法語法。什麼原因導致「擴展方法不能動態調度」在這裏?
現在,我知道如何解決這個問題,但我試圖更好地理解錯誤本身。我有我正在建設的課程來利用Dapper。最後,我將提供更多的自定義功能,使我們的數據訪問類型更加簡化。特別是在追蹤和東西建設。然而,現在它是如此簡單:
public class Connection : IDisposable
{
private SqlConnection _connection;
public Connection()
{
var connectionString = Convert.ToString(ConfigurationManager.ConnectionStrings["ConnectionString"]);
_connection = new SqlConnection(connectionString);
_connection.Open();
}
public void Dispose()
{
_connection.Close();
_connection.Dispose();
}
public IEnumerable<dynamic> Query(string sql, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
{
// this one works fine, without compile error, so I understand how to
// workaround the error
return Dapper.SqlMapper.Query(_connection, sql, param, transaction, buffered, commandTimeout, commandType);
}
public IEnumerable<T> Query<T>(string sql, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
{
// this one is failing with the error
return (IEnumerable<T>)_connection.Query(sql, param, transaction, buffered, commandTimeout, commandType);
}
}
但有趣的是,如果我只需發出這樣的聲明:
_connection.Query("SELECT * FROM SomeTable");
它編譯就好了。
那麼,有人可以幫助我理解,爲什麼利用這些其他方法中的相同超載失敗,出現該錯誤?
@pst,夠公平的,它沒有在技術上拋出。 – 2013-03-13 16:37:25
爲什麼你需要動態參數,而不是對象參數?你沒有對它進行任何操作或方法調用。你是? – 2013-03-13 16:40:12