简体   繁体   English

将事件添加到集合中,然后触发该集合中的所有事件C#

[英]Add Event to collection then fire all Events in that collection C#

I'm starting to think I'm delving into the impossible. 我开始认为我正在研究不可能的事情。

So basically when a property is changed it will fire an event: 因此,基本上,当属性更改时,它将触发一个事件:

private DateTime _DateFrom;

public DateTime DateFrom
{
    get
    {
        return _DateFrom;
    }
    set
    {
        var oldValue = _DateFrom;
        _DateFrom = value;

        if (_DateFrom != oldValue)
        {
            //GetPropertyName is an extension method 
            //Used Specifically for notifying of priority changed fields
            NotifyPropertyChanged(ReflectionTypes.GetPropertyName(() => this._DateFrom));
        }
    }
}

Once subscribed will fire and then pass to a switch statement that determines which other events to fire. 订阅后将触发,然后传递到switch语句,该语句确定要触发的其他事件。 Because an event can appear twice, I would like to add an event to a collection and then fire all those events in the collection. 因为一个事件可以出现两次,所以我想将一个事件添加到一个集合中,然后触发该集合中的所有那些事件。

public void OnOpportuntiyPropertyChanged(object sender, PropertyChangedEventArgs e)
{

    //Event Collection HERE?

    switch (e.PropertyName)
    {
            //Remove hard coded strings and replace with reflection
        case "DateFrom":
            //Event Name + Add to Event Collection
            //Event Name + Add to Event Collection
            break;
        case "SomeOtherChange":
            //Event Name + Add to Event Collection
            break;
        case "MoreChanges":
            //Event Name + Add to Event Collection
            //Event Name + Add to Event Collection
            //Event Name + Add to Event Collection
            break;
    }

    //Events will fire with successful subscription(from another location)
}

I cannot seem to get my head around this one, any help greatly appreciated! 我似乎无法解决这个问题,任何帮助都将不胜感激! So to re-cap I would like to declare an events collection then iterate through the switch and then add to collection. 因此,要概括一下,我想声明一个事件集合,然后遍历开关,然后添加到集合中。 Once the switch process has finished, I would like to fire all events in the collection. 切换过程完成后,我想触发集合中的所有事件。

//EDITS //编辑

So I'm hoping that explaining the process it will clear things up a little. 因此,我希望解释一下该过程可以使事情有所改善。

So on the UI layer within a form, a user changes the "DateFrom" field which then passes up to the "DateFrom" property (Opportunity Class). 因此,在表单的UI层上,用户更改了“ DateFrom”字段,然后该字段传递给“ DateFrom”属性(机会类)。 As this is a business critical change (Custom Travel CRM) an event needs to fire (NotifyPropertyChanged). 由于这是一项关键业务变更(Custom Travel CRM),因此需要触发一个事件(NotifyPropertyChanged)。

Once we hit the NotifyPropertyChanged event, it needs to determine which property has been changed. 遇到NotifyPropertyChanged事件后,它需要确定已更改哪个属性。 The idea is to snap in more than one business critical event hence adding to a collection. 想法是捕捉多个业务关键事件,从而增加一个集合。

The main reason for the collection is because more than one property type in the switch can have the same event and I don't want to fire those twice. 进行收集的主要原因是因为开关中的多个属性类型可以具有相同的事件,而我不想将它们触发两次。

I hope this is cleared a few things up and more then happy to embark on a new path, if the "said" solution is not a appropriate. 我希望这可以解决一些问题,如果“所述”解决方案不合适,那么我很乐意走上一条新道路。

In the original spec I forced the idea of events, so ideally need to make this work in the most extensible manner. 在最初的规范中,我强行采用了事件的概念,因此理想情况下,需要以最可扩展的方式进行这项工作。 Adding events to a collection was an idea inspired from the project manager. 将事件添加到集合中是受项目经理启发的想法。

Regards, 问候,

I'm asking how do i create the collection, how do add to the collection and how to fire all those in the collection 我问我如何创建集合,如何添加到集合以及如何触发集合中的所有集合

I think you can do what you want using a collection of Action . 我认为您可以使用Action集合来完成您想要的事情。 Here is the code: 这是代码:

//(1)create collection
List<Action> actions = new List<Action>();

switch (e.PropertyName)
{
    case "DateFrom":
        //(2)add to the collection
        actions.Add(new Action(() => {/* What you want to do in this case */ }));

    //....
}

//(3)fire all those in the collection
foreach (var item in actions)
{
    item();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM