A take on anonymous methods in VB.Net
by liquidpooled on Apr.23, 2008, under Microsoft, Visual Studio 2005
One of my colleagues has been thrust into the world of VB.Net (from C#) and has been lamenting the lack of anonymous methods in Visual Basic. Here is his implementation.
Anonymous method call:
Dim pred As New GenericPredicate(Of VendorData)(vd.VendorDataID, "VendorDataID") Dim found As VendorData = Me.vendor.Data.Find(AddressOf pred.Match)
Generic Class:
Imports System.Reflection
<Serializable()> _
Public Class GenericPredicate(Of T)
#
Region "Member Methods"
Private _value As Object
Private _property As String
#
End Region
#
Region "Constructors"
Public Sub New()
Me._value = Nothing
Me._property = String.Empty
End Sub
Public Sub New(ByVal val As Object, ByVal prop As String)
Me._value = val
Me._property = prop
End Sub
#
End Region
#
Region "Public Properties"
Public Property Value() As Object
Get
Return _value
End Get
Set(ByVal value As Object)
_value = value
End Set
End Property
Public Property Prop() As String
Get
Return _property
End Get
Set(ByVal value As String)
_property = value
End Set
End Property
#
End Region
#
Region "Functions"
Public Function Match(ByVal p As T) As Boolean
Dim t As Type = p.GetType()
Dim prop As PropertyInfo = t.GetProperty(Me._property)
Dim val As Object = prop.GetValue(p, Nothing)
Return val.Equals(Me._value)
End Function
#End Region
End
Class
Just one more way to do it.
No comments for this entry yet...