【发布时间】:2021-05-26 23:37:49
【问题描述】:
我在类上使用了 2 个泛型类型约束,我可以在 C# 代码中获取这两种类型的所有成员,但 vb.net 中的相同实现只允许访问第一个类型约束的成员。
似乎我在 vb.net 中使用了不正确的语法,但不确定。
C# 示例代码
public class MainCaller<T> where T : ITest, ITest2
{
public void MainCallerTest()
{
List<T> c = new List<T>();
//Note:- here I am able to access the member of both the interfaces
var x = c.FirstOrDefault(o => o.TestID == 1 && o.CommonID == 2);
}
}
public interface ITest
{
int TestID { get; set; }
}
public interface ITest2
{
int CommonID { get; set; }
}
public class ClassTest : ITest2, ITest
{
public int TestID { get; set ; }
public int CommonID { get; set; }
}
VB.net 示例代码
Friend Class MainCaller(Of T As ITest, ITest2)
Public Sub MainCallerTest()
Dim c As List(Of T) = New List(Of T)()
''Note:- here I am able to access the member of only ITest interface
'' not the ITest2. (o.CommonID) is not accessible in below code
Dim x = c.FirstOrDefault(Function(o) o.TestID = 1 Or o.CommonID = 2)
End Sub
End Class
Interface ITest
Property TestID As Integer
End Interface
Interface ITest2
Property CommonID As Integer
End Interface
Public Class ClassTest
Implements ITest2, ITest
Public Property TestID As Integer Implements ITest.TestID
Get
Throw New NotImplementedException()
End Get
Set(value As Integer)
Throw New NotImplementedException()
End Set
End Property
Public Property CommonID As Integer Implements ITest2.CommonID
Get
Throw New NotImplementedException()
End Get
Set(value As Integer)
Throw New NotImplementedException()
End Set
End Property
End Class
【问题讨论】:
-
我一直用这个转换器...converter.telerik.com
-
关于代码转换器,我强烈建议您从有形软件解决方案下载并安装 Instant VB 和/或 Instant C#。免费版对您可以转换的代码量有限制,但通常对大多数人来说已经足够了。
标签: c# vb.net loops type-constraints