gpt4 book ai didi

c# - 由于约束错误,无法在 C# 中实现 VB 接口(interface)

转载 作者:太空狗 更新时间:2023-10-29 18:35:08 24 4
gpt4 key购买 nike

我无法弄清楚这一点,我也不知道为什么。对不起,这个问题不好,我在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# 中实现接口(interface)时,出现以下错误。

The constraints for type parameter 'T' of method ExecuteSQL(string, bool, params System.Data.SqlClient.SqlParameter[]) must match the constraints for type parameter 'T' of interface method IFoo.ExecuteSQL(string, bool, params System.Data.SqlClient.SqlParameter[])'. Consider using an explicit interface implementation instead

这是接口(interface)的 C# 实现。不确定为什么我在使用时会出现该错误:

where T : struct

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();
}
}

enter image description here

最佳答案

VB 和 C# 编译器在将泛型类型参数约束为值类型时生成不同的 IL(请注意 C# 编译器生成的附加 .ctor([mscorlib]System.ValueType 约束)。

VB:

instance !!T ExecuteSQL<valuetype T>(string sql,
bool useTransaction,
class [System.Data]System.Data.SqlClient.SqlParameter[] parameters) cil managed

C#:

instance !!T ExecuteSQL<valuetype .ctor ([mscorlib]System.ValueType) T>(string sql,
bool useTransaction,
class [System.Data]System.Data.SqlClient.SqlParameter[] parameters) cil managed

不同的方法签名导致错误消息指示的不匹配。

我认为要让 VB 编译器生成类似的东西,您还必须添加 New 约束 - 这是编译器不允许的。因此,据我所知,没有办法绕过显式实现该接口(interface)。

(我使用 C# 5 编译器检查了这个,而不是使用 VS 2015 的新 Roslyn 编译器,它的行为可能不同)

更新

正如@Luisgrs 所指出的,VS 2015 中包含的新 Roslyn VB 编译器现在生成通用约束的方式与 C# 编译器相同。因此,一种解决方案是更新您的编译器/Visual Studio 版本。

关于c# - 由于约束错误,无法在 C# 中实现 VB 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34292793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com