Created
February 18, 2011 10:07
-
-
Save grumpydev/833489 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Pipelines | |
| { | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| public class CancellablePipeline<TPipelineDelegateType, TReturnType> | |
| where TReturnType : class | |
| { | |
| public CancellablePipeline() | |
| { | |
| if (!typeof(Delegate).IsAssignableFrom(typeof(TPipelineDelegateType))) | |
| { | |
| throw new ArgumentException("TPipelineDelegateType must be of a delegate type", "TPipelineDelegateType"); | |
| } | |
| this.pipelineItems = new List<TPipelineDelegateType>(); | |
| } | |
| private List<TPipelineDelegateType> pipelineItems; | |
| public IEnumerable<TPipelineDelegateType> PipelineItems | |
| { | |
| get | |
| { | |
| return this.pipelineItems.AsReadOnly(); | |
| } | |
| } | |
| public TReturnType Invoke(params object[] args) | |
| { | |
| TReturnType returnValue = null; | |
| using (var enumerator = this.PipelineItems.Select(i => i as Delegate).GetEnumerator()) | |
| { | |
| while (returnValue == null && enumerator.MoveNext()) | |
| { | |
| returnValue = enumerator.Current.DynamicInvoke(args) as TReturnType; | |
| } | |
| } | |
| return returnValue; | |
| } | |
| public void AddItemToStartOfPipeline(TPipelineDelegateType item) | |
| { | |
| this.InsertItemAtPipelineIndex(0, item); | |
| } | |
| public void AddItemToEndOfPipeline(TPipelineDelegateType item) | |
| { | |
| this.pipelineItems.Add(item); | |
| } | |
| public void InsertItemAtPipelineIndex(int index, TPipelineDelegateType item) | |
| { | |
| this.pipelineItems.Insert(index, item); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment