如何从传递的委托中查找同一类中的方法

发布于 2024-12-11 16:45:12 字数 1629 浏览 0 评论 0原文

我有一个带有自定义属性的方法,该方法列出了它可以调用的同一类中的其他方法。我将被调用的方法传递给另一个类函数,该函数将读取属性并选择属性中指定的方法之一并返回其委托。如果可能的话,我很难弄清楚如何通过反射或事件来做到这一点。

<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 技术交流群。

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

发布评论

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

评论(1

宫墨修音 2024-12-18 16:45:12

更新以避免中间包装器委托

我的 VB 很生锈,所以我使用 C#:

var methodNames = similarAttr.ChildBehaviors;
var randomMethodStr = methodNames[new Random().Next(methodNames.Length)];
return (Action)Delegate.CreateDelegate(source.Target, randomMethodStr);

我应该指出这里有代码味道:您可能需要考虑为什么您尝试以这种方式调用方法,并思考是否有更好的设计实践来实现您想要的。

Updated to avoid intermediate wrapper delegate

My VB is rusty, so I'm using C#:

var methodNames = similarAttr.ChildBehaviors;
var randomMethodStr = methodNames[new Random().Next(methodNames.Length)];
return (Action)Delegate.CreateDelegate(source.Target, randomMethodStr);

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.

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