Click or drag to resize

ConversionCache Class

Singleton that contains the cache of conversion factors used throughout the Quantitative assembly.
Inheritance Hierarchy
SystemObject
  InnerDrive.QuantitativeConversionCache

Namespace: InnerDrive.Quantitative
Assembly: InnerDrive.Quantitative (in InnerDrive.Quantitative.dll) Version: 5.0.8475.0
Syntax
C#
public class ConversionCache

The ConversionCache type exposes the following members.

Properties
 NameDescription
Public propertyStatic memberInstance Gets the running instance of ConversionCache.
Public propertyRegistrations Returns a read-only copy of the internally-held list of registrations.
Top
Methods
 NameDescription
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Public methodExists Gets an indication of whether a conversion exists between the Type of Unit in sourceType to targetType.
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
Public methodGetFactor Gets the factor by which a measurement in one Unit must be multiplied to convert it to another Unit of the same measurement.
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Public methodGetTypeGets the Type of the current instance.
(Inherited from Object)
Public methodIsRegistered Gets an indication of whether a particular Type is registered in the cache.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
Public methodCode exampleRegister Retrieves the conversion factors from a Unit and adds them to the cache, along with their inverse factors.
Public methodToStringReturns a string that represents the current object.
(Inherited from Object)
Top
Remarks
Implementors of Unit classes are advised to populate the Conversions list with at least the minimum conversion factors required to use the unit.
Example
This is a typical use of the ConversionCache class (taken from the Area.ConvertTo(Unit) member). Notice that the conversion factors are "lazy-loaded" i.e., the units are not registered with the cache until a conversion actually takes place.
C#
var cache = ConversionCache.Instance;
cache.Register(Unit);
cache.Register(target);

double factor = 0d;
double converted = double.NaN;

Type sourceType = Unit.GetType();
Type targetType = target.GetType();
if (cache.Exists(sourceType, targetType))
{
    factor = cache.GetFactor(sourceType, targetType);
    converted = Value * factor;
}
else if (cache.Exists(sourceType, typeof(MeterSquare)))
{
    double meterFactor = cache.GetFactor(sourceType, typeof(MeterSquare));
    double meters = this.Value * meterFactor;
    factor = cache.GetFactor(typeof(MeterSquare), targetType);
    converted = meters * factor;
}

return new Area(converted, target);
See Also