Changeset 10651


Ignore:
Timestamp:
03/24/14 16:59:40 (5 years ago)
Author:
pfleck
Message:

#2152:

  • Lookup for appropriate Interpreter now causes an InvalidOperationException if no or more than one interpreter is found.
  • Put InterpreterDataType extraction into new method for better readability.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/VehicleRoutingProblem.cs

    r10435 r10651  
    389389
    390390    public void Load(VRPData data) {
    391       Type interpreterType = typeof(IVRPDataInterpreter<>).MakeGenericType(data.GetType());
    392       var interpreters = ApplicationManager.Manager.GetInstances(interpreterType);
    393       IVRPDataInterpreter interpreter = null;
    394       foreach (object i in interpreters) {
    395         var parentInterfaces = i.GetType().BaseType.GetInterfaces();
    396         var interfaces = i.GetType().GetInterfaces().Except(parentInterfaces);
    397         var interpreterInterface = interfaces.First(j => typeof(IVRPDataInterpreter).IsAssignableFrom(j));
    398         var interpreterDataType = interpreterInterface.GetGenericArguments()[0];
    399         if (interpreterDataType == data.GetType()) {
    400           interpreter = i as IVRPDataInterpreter;
    401           break;
    402         }
    403       }
    404 
    405       if (interpreter == null)
    406         throw new ArgumentException("No interpreter found for the VRP type");
    407       Load(data, interpreter);
     391      var interpreterDataType = data.GetType();
     392      var interpreterType = typeof(IVRPDataInterpreter<>).MakeGenericType(interpreterDataType);
     393
     394      var interpreters = ApplicationManager.Manager.GetTypes(interpreterType);
     395
     396      var concreteInterpreter = interpreters.Single(t => GetInterpreterDataType(t) == interpreterDataType);
     397
     398      Load(data, (IVRPDataInterpreter)Activator.CreateInstance(concreteInterpreter));
     399    }
     400
     401    private Type GetInterpreterDataType(Type type) {
     402      var parentInterfaces = type.BaseType.GetInterfaces();
     403      var interfaces = type.GetInterfaces().Except(parentInterfaces);
     404
     405      var interpreterInterface = interfaces.Single(i => typeof(IVRPDataInterpreter).IsAssignableFrom(i));
     406      return interpreterInterface.GetGenericArguments()[0];
    408407    }
    409408
Note: See TracChangeset for help on using the changeset viewer.