arrays - 是否可以在 vb.net 中创建一个数组或函数集合?
问题描述
我正在编写一个非常基本的功能,该功能将入站电子邮件分类到组邮箱中,根据他们打算被谁阅读到文件夹中。这是一件愚蠢的事情,因为我们都有自己的电子邮件地址,但公司经常做愚蠢的事情!
确定“电子邮件是给谁”的标准非常复杂,所以我正在编写一系列简单的函数来返回它是否“给”特定的人
Function IsRichard(msg As Outlook.MailItem) As Boolean
...
End Function
Function IsTim(msg As Outlook.MailItem) As Boolean
...
End Function
目前,我正在一个控制功能中按顺序运行所有这些Function WhoIsItFor()
。但是,这个子现在变得有点长而且笨拙,所以我想知道是否可以定义一个数组或函数集合,以便我可以按照以下方式做一些事情:
Const AllFunctions as Function () = {IsRichard, IsTim ...}
Function WhoIsItFor(msg as Outlook.MailItem) as String
For Each thisFunction as Function in AllFunctions
if thisFunction(msg) then
return (thisFunction.name)
end if
next
end function
解决方案
您应该使用 AddressOf 运算符并使用委托 Func 定义数组类型:
Dim AllFunctions() As Func(Of Outlook.MailItem, Boolean) = {AddressOf IsRichard, AddressOf IsTim}
Function WhoIsItFor(msg As Outlook.MailItem) As String
For Each thisFunction As Func(Of Outlook.MailItem, Boolean) In AllFunctions
If thisFunction(msg) Then
Return thisFunction.Method.Name
End If
Next
Return Nothing
End Function
推荐阅读
- amazon-web-services - 使用 API 发布到 Greengrass 主题
- c - Scanf 在使用互斥锁的多处理中无法正常工作
- crystal-reports-2016 - 如何在水晶报表中找到父子关系中各个组的总和
- javascript - 尝试导入 React-Gridstack 时出错
- c# - 如何用DataTable C#中的特殊字符替换字符串
- python - 根据距离矩阵找出每个元素与质心之间的距离?
- javascript - Typescript - 读取并返回与特定参数匹配的所有查询参数值
- javascript - 在单击 GTM 时获取另一个子元素
- javascript - Web 编程中服务器端的编程到底是什么
- sql - 表格中的 SQL PNG