如何在.NET 2.0中编写一个简单的类似Expression的类?

发布于 2024-09-08 12:31:14 字数 1282 浏览 7 评论 0原文

我目前正在使用 .NET 2.0 Visual Basic。当前项目是一个 Active Directory Wrapper 类库,其中有一个 Searcher(Of T) 通用类,我希望用它来搜索底层目录中的对象。

在这个 Searcher(Of T) 类中,我有以下方法:

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...

我最感兴趣的是 Find() 方法,我可以向该方法传递属性和值,并且想要从此 filter() ParamArray 参数解析我的 LDAP 查询。实际上,我能弄清楚的是:

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

但我希望能够向我的用户提供的是:

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

简而言之,我想向我的用户提供某种 Expression 功能,除非它工作量太大了,因为这个项目不是最重要的,而且我没有两年的时间来开发它。我认为我应该做的更好的事情是编写类似 CustomExpression 的东西,可以将参数传递给某些函数或子函数。

感谢您提供任何可能使我实现目标的建议!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2024-09-15 12:31:14

有趣的问题。这是一个依赖于语言的功能,因此如果没有 IDE/编译器的一些巧妙技巧,我不会看到这种情况发生。

不过,您可以在 Find 方法上进行可选重载(vb.net 对此很有用),然后手动创建搜索字符串以获取结果。

最后,您可以使用 lambda 函数,但仅限于 .net 3.5 及更高版本。即便如此,它仍然需要您的搜索器公开一组初步数据,以便您可以恢复表达式树并构建查找字符串。

更新

我刚刚玩弄反射,看看是否可以检索传递的参数,并根据它们是否存在动态构建一个字符串。这似乎不可能,因为编译的代码不引用这些名称。

刚刚使用的代码是:

'-- 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

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

令人烦恼的是,它不可能(容易)恢复发送的值,所以我们必须坚持建立以非动态方式字符串。

一个简单的可选方法如下所示:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文