TemparatureConverter/TemparatureConverter.cs

31 lines
1.1 KiB
C#
Raw Normal View History

2022-12-01 15:03:49 +03:00
public class TemparatureConverter
{
public Dictionary<string, ITemperatureUnit> Dictionary { get; private set; } = null;
public TemparatureConverter()
{
IEnumerable<Type> temparatureUnitTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes()).Where(type => type.GetInterfaces().Contains(typeof(ITemperatureUnit)));
Dictionary = new Dictionary<string, ITemperatureUnit>(temparatureUnitTypes.Count());
foreach (var temparatureUnitType in temparatureUnitTypes)
Dictionary.Add(temparatureUnitType.Name, Activator.CreateInstance(temparatureUnitType) as ITemperatureUnit);
}
public float Convert(float value, ITemperatureUnit from, ITemperatureUnit to)
{
float celciusValue = from.ToCelcius(value);
return to.FromCelcius(celciusValue);
}
// public List<float> ConvertToAll(float value, ITemperatureUnit from)
// {
// List<float> list = new List<float>(Dictionary.Count);
// foreach (var toUnitPair in Dictionary)
// list.Add(Convert(value, from, toUnitPair.Value));
// return list;
// }
}