在LINQ Where中使用带有AddressOf的NOT运算符

Tim*_*ler 1 linq vb.net

我想知道是否有一种方法在LINQ Where语句中使用布尔NOT运算符时使用引用方法作为其函数,因为我认为总是有方法或布尔测试的好编程实践尽可能检查是否为正(例如,最好命名一个布尔变量/方法IsHappyIsMad,而不是NotIsHappy)

到目前为止,我有以下代码:

Dim DynClass As Object
Dim propInfos As List(Of PropertyInfo)
...
'determine all our current properties which are not primitives or strings
propInfos = DynClass.GetType.GetProperties.Where(AddressOf NotIsPrimitiveOrStringType).ToList
Run Code Online (Sandbox Code Playgroud)

然后由Where引用的方法:

Public Function IsPrimitiveOrStringType(p As PropertyInfo) As Boolean
    Return Not p.PropertyType.IsPrimitive And Not p.PropertyType.Name = GetType(String).Name
End Function
Public Function NotIsPrimitiveOrStringType(p As PropertyInfo) As Boolean
    Return Not IsPrimitiveOrStringType(p)
End Function
Run Code Online (Sandbox Code Playgroud)

DynClass可以是任何对象,并传递给该方法.

有没有更优雅的方法来实现这一点,因为我需要在我的应用程序的其他地方重用IsPrimitiveOrStringType功能?

Jam*_*rpe 5

您可以使用Lambda表达式来使用普通Not运算符:

propInfos = DynClass.GetType.GetProperties.Where(Function(p) Not IsPrimitiveOrStringType(p)).ToList
Run Code Online (Sandbox Code Playgroud)