Changeset 5843


Ignore:
Timestamp:
03/28/11 17:22:36 (9 years ago)
Author:
mkommend
Message:

#1049: Added code to create default values in ValueParameters if possible.

Location:
trunk/sources/HeuristicLab.Parameters/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Parameters/3.3/FixedValueParameter.cs

    r5809 r5843  
    3131  [Item("FixedValueParameter", "A parameter whose value is defined in the parameter itself and cannot be set.")]
    3232  [StorableClass]
    33   public class FixedValueParameter<T> : ValueParameter<T>, IFixedValueParameter<T> where T : class,IItem {
     33  public class FixedValueParameter<T> : ValueParameter<T>, IFixedValueParameter<T> where T : class,IItem, new() {
    3434
    3535    public override T Value {
     
    4444    protected FixedValueParameter(FixedValueParameter<T> original, Cloner cloner) : base(original, cloner) { }
    4545
    46     public FixedValueParameter(string name) : base(name, default(T)) { }
    47     public FixedValueParameter(string name, bool getsCollected) : base(name, default(T), getsCollected) { }
     46    public FixedValueParameter() : base() { }
     47    public FixedValueParameter(string name) : base(name) { }
     48    public FixedValueParameter(string name, bool getsCollected) : base(name, getsCollected) { }
    4849    public FixedValueParameter(string name, T value) : base(name, value) { }
    4950    public FixedValueParameter(string name, T value, bool getsCollected) : base(name, value, getsCollected) { }
    50     public FixedValueParameter(string name, string description) : base(name, description, default(T)) { }
    51     public FixedValueParameter(string name, string description, bool getsCollected) : base(name, description, default(T), getsCollected) { }
     51    public FixedValueParameter(string name, string description) : base(name, description) { }
     52    public FixedValueParameter(string name, string description, bool getsCollected) : base(name, description, getsCollected) { }
    5253    public FixedValueParameter(string name, string description, T value) : base(name, description, value) { }
    5354    public FixedValueParameter(string name, string description, T value, bool getsCollected) : base(name, description, value, getsCollected) { }
  • trunk/sources/HeuristicLab.Parameters/3.3/ValueParameter.cs

    r5445 r5843  
    2121
    2222using System;
     23using System.Reflection;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     
    4344    protected ValueParameter(bool deserializing) : base(deserializing) { }
    4445    protected ValueParameter(ValueParameter<T> original, Cloner cloner) : base(original, cloner) { }
     46
    4547    public ValueParameter() : base() { }
    46     public ValueParameter(string name) : base(name) { }
    47     public ValueParameter(string name, bool getsCollected) : base(name, getsCollected) { }
     48    public ValueParameter(string name) : base(name) { base.Value = CreateDefaultValue(); }
     49    public ValueParameter(string name, bool getsCollected) : base(name, getsCollected) { base.Value = CreateDefaultValue(); }
    4850    public ValueParameter(string name, T value) : base(name, value) { }
    4951    public ValueParameter(string name, T value, bool getsCollected) : base(name, value, getsCollected) { }
    50     public ValueParameter(string name, string description) : base(name, description) { }
    51     public ValueParameter(string name, string description, bool getsCollected) : base(name, description, getsCollected) { }
     52    public ValueParameter(string name, string description) : base(name, description) { base.Value = CreateDefaultValue(); }
     53    public ValueParameter(string name, string description, bool getsCollected) : base(name, description, getsCollected) { base.Value = CreateDefaultValue(); }
    5254    public ValueParameter(string name, string description, T value) : base(name, description, value) { }
    5355    public ValueParameter(string name, string description, T value, bool getsCollected) : base(name, description, value, getsCollected) { }
     56
     57    protected T CreateDefaultValue() {
     58      ConstructorInfo defaultConstructor = typeof(T).GetConstructor(Type.EmptyTypes);
     59      if (defaultConstructor != null)
     60        return (T)defaultConstructor.Invoke(new object[0]);
     61      return null;
     62    }
    5463
    5564    public override IDeepCloneable Clone(Cloner cloner) {
Note: See TracChangeset for help on using the changeset viewer.