【发布时间】:2015-12-15 15:15:51
【问题描述】:
我无法弄清楚,也不知道为什么。对不起这个问题不好,我在VB中有以下界面
Public Interface IFoo
Sub ExecuteSQL(sql As String, ParamArray parameters() As SqlParameter)
Sub ExecuteSQL(sql As String, useTransaction As Boolean, ParamArray parameters() As SqlParameter)
Function ExecuteSQLAsync(sql As String, ParamArray parameters() As SqlParameter) As Task
Function ExecuteSQLAsync(sql As String, useTransaction As Boolean, ParamArray parameters() As SqlParameter) As Task
Function ExecuteSQL(Of T As Structure)(sql As String, ParamArray parameters() As SqlParameter) As T
Function ExecuteSQL(Of T As Structure)(sql As String, useTransaction As Boolean, ParamArray parameters() As SqlParameter) As T
Function ExecuteSQLAsync(Of T As Structure)(sql As String, ParamArray parameters() As SqlParameter) As Task(Of T)
Function ExecuteSQLAsync(Of T As Structure)(sql As String, useTransaction As Boolean, ParamArray parameters() As SqlParameter) As Task(Of T)
End Interface
当我在 C# 中实现接口时,出现以下错误。
方法 ExecuteSQL(string, bool, params System.Data.SqlClient.SqlParameter[]) 的类型参数 'T' 的约束必须匹配接口方法 IFoo.ExecuteSQL(string, bool 的类型参数 'T' 的约束, 参数 System.Data.SqlClient.SqlParameter[])'。考虑改用显式接口实现
这是接口的 C# 实现。不知道为什么我在使用时会收到该错误:
其中 T : 结构
public class Foo : IFoo
{
public T ExecuteSQL<T>(string sql, bool useTransaction, params System.Data.SqlClient.SqlParameter[] parameters) where T : struct
{
throw new NotImplementedException();
}
public T ExecuteSQL<T>(string sql, params System.Data.SqlClient.SqlParameter[] parameters) where T : struct
{
throw new NotImplementedException();
}
public void ExecuteSQL(string sql, bool useTransaction, params System.Data.SqlClient.SqlParameter[] parameters)
{
throw new NotImplementedException();
}
public void ExecuteSQL(string sql, params System.Data.SqlClient.SqlParameter[] parameters)
{
throw new NotImplementedException();
}
public Task<T> ExecuteSQLAsync<T>(string sql, bool useTransaction, params System.Data.SqlClient.SqlParameter[] parameters) where T : struct
{
throw new NotImplementedException();
}
public Task<T> ExecuteSQLAsync<T>(string sql, params System.Data.SqlClient.SqlParameter[] parameters) where T : struct
{
throw new NotImplementedException();
}
public Task ExecuteSQLAsync(string sql, bool useTransaction, params System.Data.SqlClient.SqlParameter[] parameters)
{
throw new NotImplementedException();
}
public Task ExecuteSQLAsync(string sql, params System.Data.SqlClient.SqlParameter[] parameters)
{
throw new NotImplementedException();
}
}
【问题讨论】:
-
这看起来很奇怪。显式实现编译虽然 -
T IFoo.ExecuteSQL<T>(string sql, params SqlParameter[] parameters) -
我刚试过这个,效果很好,我想我们一定错过了其他东西。
-
@stuartd 我明白这一点,但为什么我需要对泛型方法进行隐式实现。我有其他不通用的方法,我能够毫无问题地实现接口。另外,如果我在没有限制的情况下从 ExecuteSQL(Of T) 更改方法,则没有问题
-
@DavidG 好主意,让我隔离问题。
-
顺便说一句,您更新的代码仍然适用于我。
标签: c# vb.net visual-studio visual-studio-2013