Esempio n. 1
0
        public override void Bind(UnitType candidate)
        {
            UnitType L = Lhs.Unit;
            UnitType R = Rhs.Unit;

            if ((L != null) && (R != null))
            {
                // result = unitL * unitR (definition)

                // E.g.: Joule = Newton * Meter
                // Newton.cs (or Meter.cs):
                //      public static Joule operator *(Newton lhs, Meter rhs) { return new Joule(lhs.Value * rhs.Value); }
                //      public static Joule operator *(Meter lhs, Newton rhs) { return new Joule(lhs.Value * rhs.Value); }
                L.AddOuterOperation(candidate, Operation, L, R);
                if (L.Name != R.Name)
                {
                    L.AddOuterOperation(candidate, Operation, R, L);
                }

                // => result / unitL = unitR
                // => result / unitR = unitL
                //
                // Joule.cs:
                //      public static Newton operator /(Joule lhs, Meter rhs) { return new Newton(lhs.Value / rhs.Value); }
                //      public static Meter operator /(Joule lhs, Newton rhs) { return new Meter(lhs.Value / rhs.Value); }
                candidate.AddOuterOperation(R, "/", candidate, L);
                if (L.Name != R.Name)
                {
                    candidate.AddOuterOperation(L, "/", candidate, R);
                }
            }
            else if ((L != null) && (Rhs.IsNumeric))
            {
                // result = unit * number (conversion)
                //
                // E.g. Centimeter = Meter * 100.0
                // Meter.cs:
                //      public static explicit operator Meter(Centimeter q) { return new Meter((Meter.Factor / Centimeter.Factor) * q.Value); }
                // Centimeter.cs:
                //      public static explicit operator Centimeter(Meter q) { return new Centimeter((Centimeter.Factor / Meter.Factor) * q.Value); }
                //
                Lhs.Bind(candidate);
            }
            else if ((Lhs.IsNumeric) && (R != null))
            {
                // result = number * unit (conversion)

                // E.g. Centimeter = 100.0 * Meter
                //  Meter.cs:
                //      public static explicit operator Meter(Centimeter q) { return new Meter((Meter.Factor / Centimeter.Factor) * q.Value); }
                //  Centimeter.cs:
                //      public static explicit operator Centimeter(Meter q) { return new Centimeter((Centimeter.Factor / Meter.Factor) * q.Value); }
                //
                Rhs.Bind(candidate);
            }
        }