vb.net/Linq:如何使用通用 类 的 Linq?
vb.net/Linq: how can I use a Linq using Generic classes?
我有下一个结构:
public class mysample
public property status as integer
end class
Dim mylist=new List(Of mysample)
Dim item as new mysample
item.status=1
mylist.add(item)
Dim item1 as new mysample
item2.status=1
mylist.add(item1)
...
我有下一个函数,它正在计算一些东西:
Function test(Of T)(newstatus as integer, mylist as List(of T)) as integer
Dim res as integer = myList.Where(Function(x) x.status=newstatus).First.status
Return res
End function
调用是我有兴趣执行的地方:test(Of mysample)(2, mylist)
我在不同的项目中有 mysample,因此它们不能在同一个项目中,因此我决定使用通用列表来进行我的 Linq 计算。
问题出在功能上,它告诉我状态不是 T 对象的成员。
我该如何解决这个问题?所有类都有状态,但我有不同的 类 并且我将名称作为通用类型传递。
classes 是否共享一个公共基础 class 或接口?如果是这样,您应该像这样在通用类型上放置一个过滤器:
Function test(Of T as CommonBaseClassOrInterface)(newstatus as integer, mylist as List(of T)) as integer
这将允许您访问 CommonBaseClassOrInterface
上的任何成员。如果他们目前不共享基础 class 或接口,您应该考虑添加一个,确保 Status
是成员。
如果由于某种原因你不能给他们一个基础 class 或接口,你仍然可以使用反射来做到这一点,但我不建议朝那个方向发展。
STATUS IS NOT MEMBER OF T OBJECT.
是的,因为你没有任何约束T
。调用
是完全合法的
test(Of Integer)(2, new list(of Integer))
这会失败,因为 Integer
没有 status
属性。您需要将 T
限制为具有 status
属性(基础 class 或通用接口)的某种类型,或者 don '使它通用:
Function test(newstatus as integer, mylist as List(of mystatus)) as integer
Dim res as integer = myList.Where(Function(x) x.status=newstatus).First.status
Return res
End function
I have mysample in different projects
你的意思是你在几个项目中有几个class名字mystatus
?那么他们不一样class。
all classes has status but I have different classes and I pass the name as generic type
至少创建一个具有 Status
属性 的接口,并使用 that 来约束 Test
中的通用参数。
我有下一个结构:
public class mysample
public property status as integer
end class
Dim mylist=new List(Of mysample)
Dim item as new mysample
item.status=1
mylist.add(item)
Dim item1 as new mysample
item2.status=1
mylist.add(item1)
...
我有下一个函数,它正在计算一些东西:
Function test(Of T)(newstatus as integer, mylist as List(of T)) as integer
Dim res as integer = myList.Where(Function(x) x.status=newstatus).First.status
Return res
End function
调用是我有兴趣执行的地方:test(Of mysample)(2, mylist)
我在不同的项目中有 mysample,因此它们不能在同一个项目中,因此我决定使用通用列表来进行我的 Linq 计算。
问题出在功能上,它告诉我状态不是 T 对象的成员。
我该如何解决这个问题?所有类都有状态,但我有不同的 类 并且我将名称作为通用类型传递。
classes 是否共享一个公共基础 class 或接口?如果是这样,您应该像这样在通用类型上放置一个过滤器:
Function test(Of T as CommonBaseClassOrInterface)(newstatus as integer, mylist as List(of T)) as integer
这将允许您访问 CommonBaseClassOrInterface
上的任何成员。如果他们目前不共享基础 class 或接口,您应该考虑添加一个,确保 Status
是成员。
如果由于某种原因你不能给他们一个基础 class 或接口,你仍然可以使用反射来做到这一点,但我不建议朝那个方向发展。
STATUS IS NOT MEMBER OF T OBJECT.
是的,因为你没有任何约束T
。调用
test(Of Integer)(2, new list(of Integer))
这会失败,因为 Integer
没有 status
属性。您需要将 T
限制为具有 status
属性(基础 class 或通用接口)的某种类型,或者 don '使它通用:
Function test(newstatus as integer, mylist as List(of mystatus)) as integer
Dim res as integer = myList.Where(Function(x) x.status=newstatus).First.status
Return res
End function
I have mysample in different projects
你的意思是你在几个项目中有几个class名字mystatus
?那么他们不一样class。
all classes has status but I have different classes and I pass the name as generic type
至少创建一个具有 Status
属性 的接口,并使用 that 来约束 Test
中的通用参数。