我有一种习俗特性,即把其他方法归入可以称之为的同一类别。 我把所谓的方法推向另一个班级功能,该功能将读作属性,选择属性中标明的方法之一,并交还其代表。 如果有可能,我有很强的时间,说明如何以反思或事件来做到这一点。
<AttributeUsage(AttributeTargets.Method, allowmultiple:=False)>
Public Class ExecuteSimiliar
Inherits System.Attribute
Public ReadOnly ChildBehaviors() As String
Public Sub New(ParamArray childBehaviors() As String)
Me.ChildBehaviors = childBehaviors
End Sub
End Class
Public Class MyMethods
<ExecuteSimiliar("JellyBeanBlack", "JellyBeanRed", "JellyBeanWhite", "JellyBeanGreen")>
Public Shared Sub JellBean()
Dim myFunction As Action = ExecuteManager.Choose(AddressOf JellBean)
myFunction.Invoke()
End Sub
Public Shared Sub JellyBeanBlack()
Do something
End Sub
Public Shared Sub JellyBeanRed()
Do something
End Sub
Public Shared Sub JellyBeanWhite()
Do something
End Sub
Public Shared Sub JellyBeanGreen()
Do something
End Sub
End Class
Public Class ExecuteManager
Public Shared Function Choose(source As Action) As Action
Dim similarAttr As ExecuteSimiliar
similarAttr = CType(Attribute.GetCustomAttribute(source.Method, GetType(ExecuteSimiliar)), ExecuteSimiliar)
Select random function name from similarAttr
Get sibling methods from source delegate
Return selected method delegate. (JellyBeanBlack, JellyBeanRed, JellyBeanWhite, JellyBeanGreen)
End Function
End Class
中间电话将进入JellyBean()和JellyBeanBlack、JellyBeanRed、JellyBean White,JellyBeanGreen在被执行经理挑选后将被援引。