English 中文(简体)
如何从通过的代表那里找到同一类别的方法
原标题:How to find methods in the same class from a passed delegate

我有一种习俗特性,即把其他方法归入可以称之为的同一类别。 我把所谓的方法推向另一个班级功能,该功能将读作属性,选择属性中标明的方法之一,并交还其代表。 如果有可能,我有很强的时间,说明如何以反思或事件来做到这一点。

<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在被执行经理挑选后将被援引。

最佳回答

http://www.un.org/Depts/d?

我的VB是虚幻的,因此我用C#:

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

我应该指出,这里有一部法典:你可能想考虑一下why,你试图以这种方式提出方法,并想到,实现你所希望的更好的设计做法。

问题回答

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签