C# vb.net中的多个泛型类型约束仅允许访问第一个类型约束成员
我在类上使用了2个泛型类型约束,我可以在C代码中获得这两个类型的所有成员,但vb.net中的同一个实现只允许访问第一个类型约束的成员 看起来我在vb.net中使用了不正确的语法,但不确定 C示例代码C# vb.net中的多个泛型类型约束仅允许访问第一个类型约束成员,c#,vb.net,loops,type-constraints,C#,Vb.net,Loops,Type Constraints,我在类上使用了2个泛型类型约束,我可以在C代码中获得这两个类型的所有成员,但vb.net中的同一个实现只允许访问第一个类型约束的成员 看起来我在vb.net中使用了不正确的语法,但不确定 C示例代码 仅使用逗号表示第二个泛型类型参数。单个参数的多个约束必须使用大括号分组: 友元类MainCallerOf T As{ITest,ITest2} 您拥有的VB代码相当于此C: 公共类MainCaller,其中T:ITest 仅使用逗号表示第二个泛型类型参数。单个参数的多个约束必须使用大括号分组: 友元
仅使用逗号表示第二个泛型类型参数。单个参数的多个约束必须使用大括号分组: 友元类MainCallerOf T As{ITest,ITest2} 您拥有的VB代码相当于此C: 公共类MainCaller,其中T:ITest
仅使用逗号表示第二个泛型类型参数。单个参数的多个约束必须使用大括号分组: 友元类MainCallerOf T As{ITest,ITest2} 您拥有的VB代码相当于此C: 公共类MainCaller,其中T:ITest
我总是用这个转换器。。。关于代码转换器,我强烈建议从有形软件解决方案下载和安装Instant VB和/或Instant C。免费版本对您可以转换的代码数量有限制,但通常对大多数人来说已经足够了。。。关于代码转换器,我强烈建议从有形软件解决方案下载和安装Instant VB和/或Instant C。免费版本对您可以转换的代码数量有限制,但通常对大多数人来说已经足够了。谢谢@jmcilhinneythanks@jmcilhinney
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; }
}
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