using System.Collections.Generic;
public class InstrumentData
public static IList<IDictionary<string,string>> InstrumentSettings = new List<IDictionary<string,string>>()
new Dictionary<string,string>()
new Dictionary<string,string>()
{"SerialNumber", "1201"},
{"ModelName", "Handheld"}
new Dictionary<string,string>()
{"SerialNumber", "1202"},
{"ModelName", "Handheld"},
new Dictionary<string,string>()
{"Serial_Number", "2001"},
new Dictionary<string,string>()
{"Serial_Number", "2002"},
{"ModelName", "Apex R50"}
public enum CompatibilityMatch
public class InstrumentType
public string ProductType { get; set; }
public string ModelName { get; set; }
public interface ILwsInstrument
int SerialNumber { get; }
string ProductType { get; }
string ModelName { get; }
IList<InstrumentType> SupportedInstruments { get; }
CompatibilityMatch GetDeviceCompatibility(string productType, string modelName);
void Initialize(IDictionary<string,string> settings);
public static void Main()
var instrumentTypes = GetInstrumentTypes();
foreach(var setting in InstrumentData.InstrumentSettings)
ILwsInstrument bestMatch = null;
foreach(var iType in instrumentTypes)
var instrumentInstance = Activator.CreateInstance(iType) as ILwsInstrument;
var compatibility = instrumentInstance.GetDeviceCompatibility(setting["ProductType"], setting["ModelName"]);
if(compatibility > CompatibilityMatch.NotCompatible)
if(bestMatch == null || bestMatch.GetDeviceCompatibility(bestMatch.ProductType, bestMatch.ModelName) < compatibility)
instrumentInstance.Initialize(setting);
bestMatch = instrumentInstance;
Console.WriteLine(bestMatch == null ? "No Match" : bestMatch.SerialNumber.ToString());
public static IList<Type> GetInstrumentTypes()
return typeof(ILwsInstrument).Assembly.GetTypes().Where(t => !t.IsAbstract && t != typeof(ILwsInstrument) && typeof(ILwsInstrument).IsAssignableFrom(t)).ToList();
public abstract class LwsInstrument : ILwsInstrument
protected IDictionary<string,string> settings { get; set; }
public int SerialNumber { get; set; }
public abstract IList<InstrumentType> SupportedInstruments { get; protected set;}
public string ProductType { get => this.settings["ProductType"]; }
public string ModelName { get => this.settings["ProductType"]; }
public CompatibilityMatch GetDeviceCompatibility(string productType, string modelName)
var typeMatches = this.SupportedInstruments.Where(i => i.ProductType.Equals(productType)).ToList();
return CompatibilityMatch.NotCompatible;
if(typeMatches.Any(i => i.ModelName.Equals(modelName)))
return CompatibilityMatch.Exact;
return CompatibilityMatch.Partial;
public abstract void Initialize(IDictionary<string,string> settings);
public class RemoteInstrument : LwsInstrument
public override IList<InstrumentType> SupportedInstruments { get; protected set;} = new List<InstrumentType>()
new InstrumentType { ProductType = "APC", ModelName = "Remote" }
public override void Initialize(IDictionary<string,string> settings)
this.settings = settings;
this.SerialNumber = int.TryParse(this.settings["Serial"], out var serial) ? serial : 0;
public class HandheldInstrument : LwsInstrument
public override IList<InstrumentType> SupportedInstruments { get; protected set;} = new List<InstrumentType>()
new InstrumentType { ProductType = "APC", ModelName = "Handheld" }
public override void Initialize(IDictionary<string,string> settings)
this.settings = settings;
this.SerialNumber = int.TryParse(this.settings["SerialNumber"], out var serial) ? serial : 0;
public class ApexInstrument : LwsInstrument
public override IList<InstrumentType> SupportedInstruments { get; protected set;} = new List<InstrumentType>()
new InstrumentType { ProductType = "APC", ModelName = "Apex" },
new InstrumentType { ProductType = "LPC", ModelName = "Apex R50" }
public override void Initialize(IDictionary<string,string> settings)
this.settings = settings;
this.SerialNumber = int.TryParse(this.settings["Serial_Number"], out var serial) ? serial : 0;