vb.net 中的多个泛型类型约束仅允许访问第一个类型约束成员
multiple generic type constraint in vb.net allows access to first type constraint members only
我在 class 上使用了 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
仅使用逗号表示第二个泛型类型参数。单个参数的多个约束必须用大括号分组:
Friend Class MainCaller(Of T As {ITest, ITest2})
您的 VB 代码相当于此 C#:
public class MainCaller<T, ITest2> where T : ITest
我在 class 上使用了 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
仅使用逗号表示第二个泛型类型参数。单个参数的多个约束必须用大括号分组:
Friend Class MainCaller(Of T As {ITest, ITest2})
您的 VB 代码相当于此 C#:
public class MainCaller<T, ITest2> where T : ITest