I am writing a open source engineering calculator application in C#.
There is a class called CalcVar, which represents a single calculator variable. They are added to a Calculator class. On construction of this CalcVar variable, it is passed an "equation" lambda function (of type Func<double>, as shown below) whose body may contain any number of CalcVar objects (which also belong to the same Calculator class), which are multiplied/added/divided/whatever together and return the value for this CalcVar object.
e.g.
class OhmsLaw : Calculator
{
CalcVar voltage;
CalcVar current;
CalcVar resistance;
public OhmsLaw() : base("Ohm's Law", "Ohm's law calculator.")
{
this.voltage = new CalcVar(() => current.RawVal*resistance.RawVal);
this.current = new CalcVar(() => voltage.RawVal / resistance.RawVal);
this.resistance = new CalcVar(() => voltage.RawVal / current.RawVal);
}
}
I want to be able to somehow work out what other CalcVar variables are used inside a given CalcVar's equation function, so I can work out the variables dependencies.
How would I go about doing this? I feel like I need to emit an event or similar when a CalcVar's RawVal is accessed, and somehow get the CalcVar in question to subscribe to these events and log which ones fire when it calls equation.Invoke().