Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18085 was 18085, checked in by abeham, 3 years ago

#2521: added algorithm and problem lookup parameters

File size: 1.6 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 abstract 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    protected ContextLookupParameter() : this("ContextLookup." + typeof(T).Name) { }
20    protected ContextLookupParameter(string name) : this(name, string.Empty) { }
21    protected ContextLookupParameter(string name, string description) : base(name, description, typeof(T)) {
22      Hidden = true;
23    }
24
25    protected override IItem GetActualValueFromContext() {
26      IItem item = null;
27      var context = ExecutionContext;
28      while (context != null) {
29        if (context.Item != null && typeof(T).IsAssignableFrom(context.Item.GetType())) {
30          item = context.Item;
31          break;
32        }
33        context = context.Parent;
34      }
35      return item;
36    }
37
38    protected override void SetActualValue(IItem value) {
39      throw new NotSupportedException("The context lookup parameter may not be used to set an item.");
40    }
41  }
42}
Note: See TracBrowser for help on using the repository browser.