Tutorial :How to write a simple Expression-like class in .NET 2.0?



Question:

I'm currently working in .NET 2.0 Visual Basic. The current project is an Active Directory Wrapper class library within which I have a Searcher(Of T) generic class that I wish to use to search the underlying directory for objects.

In this Searcher(Of T) class I have the following methods:

Private Function GetResults() As CustomSet(Of T)  Public Function ToList() As CustomSet(Of T)  Public Function Find(ByVal ParamArray filter() As Object) As CustomSet(Of T)  // And some other functions here...  

The one that interests me the most is the Find() method to which I can pass property and values and would like to parse my LDAP query from this filter() ParamArray parameter. Actually, all I can figure out is this:

Public Sub SomeSub()      Dim groupSearcher As Searcher(Of Group) = New Searcher(Of Group)()      Dim groupsSet as CustomSet(Of Group) = groupSearcher.Find("Name=someName", "Description=someDescription")        // Working with the result here...  End Sub  

But what I want to be able to offer to my users is this:

Public Sub SomeSub()      Dim groupSearcher As Searcher(Of Group) = New Searcher(Of Group)()      Dim groupsSet As CustomSet(Of Groupe) = groupSearcher.Find(Name = "someName", Guid = someGuid, Description = "someDescription")        // And work with the result here...  End Sub  

In short, I want to offer some kind of Expression feature to my users, unless it is too much work, as this project is not the most important one and I don't have like 2 years to develop it. I think that the better thing I should do is to write something like CustomExpression that could be passed in parameters to some functions or subs.

Thanks for any suggestions that might bring me to my goal!


Solution:1

Interesting question. This is a language dependent feature, so I don't see this happening without some clever trickery of the IDE/compiler.

You could however have optional overloads on your Find method (vb.net is good for this), then make the search string manually to obtain the result.

Finally you could make use of lambda functions, but only in .net 3.5 and above. Even still, it would require your searcher to expose a preliminary set of data so you can recover the expression tree and build up the find string.

UPDATE

I've just been playing around with Reflection to see if I can retrieve the parameters passed, and build up a string dynamically depending on if they exist. This doesn't appear to be possible, due to the fact that compiled code doesn't reference the names.

This code just used was:

'-- Get all the "parameters"  Dim m As MethodInfo = GetType(Finder).GetMethod("Find")  Dim params() As ParameterInfo = m.GetParameters()  '-- We now have a reference to the parameter names, like Name and Description  

Hmm. http://channel9.msdn.com/forums/TechOff/259443-Using-SystemReflection-to-obtain-parameter-values-dynamically/

Annoyingly it's not (easily) possible to recover the values sent, so we'll have to stick with building up the string in a non-dynamic fashion.

A simple optional method would look like:

Public Sub Find( _                 Optional ByVal Name As String = "", _                 Optional ByVal Description As String = "")        Dim query As String = String.Empty      If Not String.IsNullOrEmpty(Name) Then          query &= "Name=" & Name          '-- ..... more go here with your string seperater.      End If  End Sub  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »