如何从传递的委托中查找同一类中的方法
我有一个带有自定义属性的方法,该方法列出了它可以调用的同一类中的其他方法。我将被调用的方法传递给另一个类函数,该函数将读取属性并选择属性中指定的方法之一并返回其委托。如果可能的话,我很难弄清楚如何通过反射或事件来做到这一点。
<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、JellyBeanWhite、JellyBeanGreen 函数之一将在被执行管理器选择后被调用。
I have a method with a custom attribute that lists other methods in the same class that it can call. I'm passing the called method to another class function that will read the attribute and pick one of the methods named in the attribute and return it's delegate. I'm having a hard time figuring out how to do this with reflection or event if it is possible.
<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
The inital call would be into JellyBean() and one of the JellyBeanBlack, JellyBeanRed, JellyBeanWhite, JellyBeanGreen functions would get invoked after being selected by the execution manager.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新以避免中间包装器委托
我的 VB 很生锈,所以我使用 C#:
我应该指出这里有代码味道:您可能需要考虑为什么您尝试以这种方式调用方法,并思考是否有更好的设计实践来实现您想要的。
Updated to avoid intermediate wrapper delegate
My VB is rusty, so I'm using C#:
I should point out that there is a code smell here: you may want to consider why you are trying to call methods this way, and think if there might be a better design practice to achieve what you want.