Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ContextLookupParameter.cs @ 17257

Last change on this file since 17257 was 17257, checked in by abeham, 5 years ago

#2521: Refactored ContextLookupParameter with suggestions from mkommend

  • Add StorableType attribute to some types
File size: 1.5 KB
Line 
1using System;
2using HEAL.Attic;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5
6namespace HeuristicLab.Parameters {
7  [Item("ContextLookupParameter", "A parameter that looks up contexts by type.")]
8  [StorableType("4ac189c8-6cf3-48fd-bf79-392d35a872db")]
9  public class ContextLookupParameter<T> : ContextParameter, IContextLookupParameter<T>
10      where T : class, IParameterizedItem {
11
12    public new T ActualValue {
13      get { return (T)base.ActualValue; }
14    }
15
16    [StorableConstructor]
17    protected ContextLookupParameter(StorableConstructorFlag _) : base(_) { }
18    protected ContextLookupParameter(ContextLookupParameter<T> original, Cloner cloner) : base(original, cloner) { }
19    public ContextLookupParameter() : base("ContextLookup." + typeof(T).Name, string.Empty, typeof(T)) {
20      Hidden = true;
21    }
22
23    public override IDeepCloneable Clone(Cloner cloner) {
24      return new ContextLookupParameter<T>(this, cloner);
25    }
26
27    protected override IItem GetActualValueFromContext() {
28      IItem item = null;
29      var context = ExecutionContext;
30      while (context != null) {
31        if (context.Item != null && typeof(T).IsAssignableFrom(context.Item.GetType())) {
32          item = context.Item;
33          break;
34        }
35        context = context.Parent;
36      }
37      return item;
38    }
39
40    protected override void SetActualValue(IItem value) {
41      throw new NotSupportedException("The context lookup parameter may not be used to set an item.");
42    }
43  }
44}
Note: See TracBrowser for help on using the repository browser.