我无法弄清楚这一点,我不知道为什么.抱歉,这是一个糟糕的问题,我在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#中实现接口时,我收到以下错误.
对于类型参数"T"方法的ExecuteSQL的(字符串,布尔,则params System.Data.SqlClient.SqlParameter [])必须为类型参数界面方法IFoo.ExecuteSQL(串的"T"的约束条件匹配,布尔,则params系统的约束.Data.SqlClient.SqlParameter [])".请考虑使用显式接口实现
这是接口的C#实现.我不确定为什么在使用时会出现错误:
其中T:struct
public class Foo : IFoo { public T ExecuteSQL(string sql, bool useTransaction, params System.Data.SqlClient.SqlParameter[] parameters) where T : struct { throw new NotImplementedException(); } public T ExecuteSQL (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 ExecuteSQLAsync (string sql, bool useTransaction, params System.Data.SqlClient.SqlParameter[] parameters) where T : struct { throw new NotImplementedException(); } public Task ExecuteSQLAsync (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(); } }
在将泛型类型参数约束为值类型时,VB和C#编译器会生成不同的IL(请注意.ctor ([mscorlib]System.ValueType
C#编译器生成的附加约束).
VB:
instance !!T ExecuteSQL(string sql, bool useTransaction, class [System.Data]System.Data.SqlClient.SqlParameter[] parameters) cil managed
C#:
instance !!T ExecuteSQL(string sql, bool useTransaction, class [System.Data]System.Data.SqlClient.SqlParameter[] parameters) cil managed
不同的方法签名导致错误消息指示的不匹配.
我认为要让VB编译器生成类似的东西,你也必须添加一个New
约束 - 这是编译器不允许的.因此,据我所知,没有办法明确实现该接口.
(我使用C#5编译器检查了这一点,没有使用可能表现不同的VS 2015的新Roslyn编译器)
正如@Luisgrs所指出的,VS 2015中包含的新Roslyn VB编译器现在以与C#编译器相同的方式生成泛型约束.因此,一种解决方案是更新编译器/ Visual Studio版本.