I have a power unit enumeration type that looks like as below.
public sealed record PowerUnit : StringEnumeration
{
public static readonly PowerUnit BTUH = new(nameof(BTUH), UnitConstants.BritishThermalUnitsPerHour);
public static readonly PowerUnit WATT = new(nameof(WATT), UnitConstants.Watt);
private static readonly Dictionary<string, PowerUnit> _byNameDictionary = GetByNameDictionaryInternal<PowerUnit>();
public static PowerUnit ByName(string name) => _byNameDictionary[name];
private PowerUnit() { }
public PowerUnit(string id, string name) : base(id, name) { }
public static PowerUnit FromUnitsNetUnit(UnitsNet.Units.PowerUnit unitsNetUnit)
=> unitsNetUnit switch
{
UnitsNet.Units.PowerUnit.BritishThermalUnitPerHour => BTUH,
UnitsNet.Units.PowerUnit.Watt => WATT,
_ => throw new ArgumentException("Invalid unit type.", nameof(unitsNetUnit))
};
// The following is internal because if 'public' it causes HC pipeline initialization to crash
internal UnitsNet.Units.PowerUnit ToUnitsNetUnit()
=> this.Id switch
{
nameof(BTUH) => UnitsNet.Units.PowerUnit.BritishThermalUnitPerHour,
nameof(WATT) => UnitsNet.Units.PowerUnit.Watt,
_ => throw new InvalidOperationException("The equivalent UnitsNet unit is unknown.")
};
}
I have a power class that looks like as below.
public class Power : UnitType<Power, PowerUnit>, IConvertibleUnit<Power, PowerUnit>
{
public Power() { }
public Power(Power source) : base(source){ }
public Power(PowerUnit unit, double scalar) : base(unit, scalar) {}
public override Power Convert(PowerUnit toUnit)
{
if (toUnit == this.Unit) return new(this);
UnitsNet.IQuantity powerQuantity = UnitsNet.Power.From(Scalar, Unit.ToUnitsNetUnit()); // Getting exception here and Unit is null
var toScalar = powerQuantity.ToUnit(toUnit.ToUnitsNetUnit()).Value;
return new(toUnit, toScalar);
}
}
Here UnitType
is an abstract class, and its signature looks like as below,
public abstract class UnitType<T0, T1> where T0: class, IConvertibleUnit<T0, T1>
where T1: StringEnumeration
{
public string UnitId { get; set; }
public T1 Unit { get; set; }
public double Scalar { get; set; }
protected UnitType() { }
protected UnitType(UnitType<T0, T1> source)
{
UnitId = source.UnitId;
Unit = source.Unit;
Scalar = source.Scalar;
}
protected UnitType(T1 unit, double scalar)
{
UnitId = unit.Id;
Unit = unit;
Scalar = scalar;
}
public abstract T0 Convert(T1 toUnit);
}
I am using this power as a column type, and one of the tables that looks like as below,
public class Space
{
//.....
public Power ElectricalPowerOverride { get; set; }
//....
}
I have a requirement where I need to get the scalar value from this ElectricalPowerOverride
for that I am accessing the same value as below,
var dhSpace = _dbContext.Spaces.SingleOrDefault(space => space.ProjectNumber == _designHubProject.ProjectNumber);
var electricalPowerLevelInSI = dhSpace.ElectricalPowerOverride.Convert(PowerUnit.WATT).Scalar; // here getting that null reference exception when accessing the scalar
Could anyone please let me know why I am getting the exception inside the Convert
method, like getting object null for Unit
when trying to access Unit.ToUnitsNetUnit()
in Power
class
Sorry for the long post, As those are necessary for the problem
Many thanks in advance!!!