Skip to content

DiGi.Unit.Classes

github-actions[bot] edited this page Jul 15, 2026 · 2 revisions

DiGi.Unit.Classes Namespace

Classes

CategoryAttribute Class

An attribute used to categorize enums, implementing serialization and naming contracts.

public sealed class CategoryAttribute : System.Attribute, DiGi.Core.Interfaces.ISerializableObject, DiGi.Core.Interfaces.ICloneableObject<DiGi.Core.Interfaces.ISerializableObject>, DiGi.Core.Interfaces.ICloneableObject, DiGi.Core.Interfaces.IObject, DiGi.Core.Interfaces.INamedObject

Inheritance System.ObjectSystem.Attribute → CategoryAttribute

Implements DiGi.Core.Interfaces.ISerializableObject, DiGi.Core.Interfaces.ICloneableObject<DiGi.Core.Interfaces.ISerializableObject>, DiGi.Core.Interfaces.ICloneableObject, DiGi.Core.Interfaces.IObject, DiGi.Core.Interfaces.INamedObject

Constructors

CategoryAttribute(CategoryAttribute) Constructor

Initializes a new instance of the CategoryAttribute class by copying an existing category attribute.

public CategoryAttribute(DiGi.Unit.Classes.CategoryAttribute category);

Parameters

category CategoryAttribute

The source category attribute to copy.

CategoryAttribute(UnitCategory) Constructor

Initializes a new instance of the CategoryAttribute class using a specified unit category.

public CategoryAttribute(DiGi.Unit.Enums.UnitCategory unitCategory);

Parameters

unitCategory UnitCategory

The unit category to assign.

CategoryAttribute(JsonObject) Constructor

Initializes a new instance of the CategoryAttribute class from a JSON object.

public CategoryAttribute(System.Text.Json.Nodes.JsonObject jsonObject);

Parameters

jsonObject System.Text.Json.Nodes.JsonObject

The JSON object containing category data.

Properties

CategoryAttribute.Name Property

Gets the name associated with the category.

public string Name { get; }

Implements Name

Property Value

System.String

CategoryAttribute.UnitCategory Property

Gets the UnitCategory enum value corresponding to the current name.

public System.Nullable<DiGi.Unit.Enums.UnitCategory> UnitCategory { get; }

Property Value

System.Nullable<UnitCategory>

Methods

CategoryAttribute.Clone() Method

Creates a clone of the current category attribute.

public DiGi.Core.Interfaces.ISerializableObject? Clone();

Implements Clone()

Returns

DiGi.Core.Interfaces.ISerializableObject
A cloned DiGi.Core.Interfaces.ISerializableObject instance.

CategoryAttribute.FromJsonObject(JsonObject) Method

Populates the current category attribute from a JSON object.

public bool FromJsonObject(System.Text.Json.Nodes.JsonObject? jsonObject);

Parameters

jsonObject System.Text.Json.Nodes.JsonObject

The JSON object to read data from.

Implements FromJsonObject(JsonObject)

Returns

System.Boolean
True if the population was successful; otherwise, false.

CategoryAttribute.ToJsonObject() Method

Converts the current category attribute to a JSON object.

public System.Text.Json.Nodes.JsonObject? ToJsonObject();

Implements ToJsonObject()

Returns

System.Text.Json.Nodes.JsonObject
A System.Text.Json.Nodes.JsonObject representation of the attribute.

Operators

CategoryAttribute.implicit operator CategoryAttribute(UnitCategory) Operator

Implicitly converts a UnitCategory value to a CategoryAttribute.

public static DiGi.Unit.Classes.CategoryAttribute implicit operator DiGi.Unit.Classes.CategoryAttribute(DiGi.Unit.Enums.UnitCategory unitCategory);

Parameters

unitCategory UnitCategory

The unit category to convert.

Returns

CategoryAttribute
A new CategoryAttribute instance.

CategoryAttribute.implicit operator CategoryAttribute(string) Operator

Implicitly converts a string name to a CategoryAttribute.

public static DiGi.Unit.Classes.CategoryAttribute implicit operator DiGi.Unit.Classes.CategoryAttribute(string name);

Parameters

name System.String

The category name to convert.

Returns

CategoryAttribute
A new CategoryAttribute instance.

FactorUnitAttribute Class

Represents an attribute that defines a unit of measurement based on a conversion factor.

public class FactorUnitAttribute : DiGi.Unit.Classes.UnitAttribute

Inheritance System.ObjectSystem.AttributeUnitAttribute → FactorUnitAttribute

Derived
LinearUnitAttribute

Constructors

FactorUnitAttribute(FactorUnitAttribute) Constructor

Initializes a new instance of the FactorUnitAttribute class by copying an existing instance.

public FactorUnitAttribute(DiGi.Unit.Classes.FactorUnitAttribute factorUnit);

Parameters

factorUnit FactorUnitAttribute

The source attribute to copy from.

FactorUnitAttribute(string, string, double) Constructor

Initializes a new instance of the FactorUnitAttribute class.

public FactorUnitAttribute(string? name, string? symbol, double factor);

Parameters

name System.String

The name of the unit.

symbol System.String

The symbol representing the unit.

factor System.Double

The conversion factor for the unit.

FactorUnitAttribute(JsonObject) Constructor

Initializes a new instance of the FactorUnitAttribute class from a JSON object.

public FactorUnitAttribute(System.Text.Json.Nodes.JsonObject jsonObject);

Parameters

jsonObject System.Text.Json.Nodes.JsonObject

The JSON object containing the attribute data.

Properties

FactorUnitAttribute.Factor Property

Gets the conversion factor used for value transformations.

public double Factor { get; }

Property Value

System.Double

Methods

FactorUnitAttribute.Clone() Method

Creates a clone of the current FactorUnitAttribute instance.

public override DiGi.Core.Interfaces.ISerializableObject? Clone();

Implements Clone()

Returns

DiGi.Core.Interfaces.ISerializableObject
A new DiGi.Core.Interfaces.ISerializableObject representing the cloned attribute.

FactorUnitAttribute.From(double) Method

Converts a value from the base unit to this specific unit by multiplying it by the factor.

public override double From(double value);

Parameters

value System.Double

The numeric value to convert.

Returns

System.Double
The converted value.

FactorUnitAttribute.To(double) Method

Converts a value from this specific unit back to the base unit by dividing it by the factor.

public override double To(double value);

Parameters

value System.Double

The numeric value to convert.

Returns

System.Double
The converted value.

LinearUnitAttribute Class

Represents an attribute for linear unit conversion that incorporates both a multiplication factor and an additive offset.

public class LinearUnitAttribute : DiGi.Unit.Classes.FactorUnitAttribute

Inheritance System.ObjectSystem.AttributeUnitAttributeFactorUnitAttribute → LinearUnitAttribute

Constructors

LinearUnitAttribute(LinearUnitAttribute) Constructor

Initializes a new instance of the LinearUnitAttribute class by copying an existing linear unit attribute.

public LinearUnitAttribute(DiGi.Unit.Classes.LinearUnitAttribute linearUnit);

Parameters

linearUnit LinearUnitAttribute

The source LinearUnitAttribute to copy from.

LinearUnitAttribute(string, string, double, double) Constructor

Initializes a new instance of the LinearUnitAttribute class with specified name, symbol, factor, and offset.

public LinearUnitAttribute(string? name, string? symbol, double factor, double offset);

Parameters

name System.String

The display name of the unit.

symbol System.String

The symbol representing the unit.

factor System.Double

The multiplication factor used for conversion.

offset System.Double

The additive offset used for conversion.

LinearUnitAttribute(JsonObject) Constructor

Initializes a new instance of the LinearUnitAttribute class from a JSON object.

public LinearUnitAttribute(System.Text.Json.Nodes.JsonObject jsonObject);

Parameters

jsonObject System.Text.Json.Nodes.JsonObject

The System.Text.Json.Nodes.JsonObject containing the unit configuration.

Properties

LinearUnitAttribute.Offset Property

Gets the additive offset used in the linear conversion calculation.

public double Offset { get; }

Property Value

System.Double

Methods

LinearUnitAttribute.Clone() Method

Creates a clone of the current linear unit attribute.

public override DiGi.Core.Interfaces.ISerializableObject? Clone();

Implements Clone()

Returns

DiGi.Core.Interfaces.ISerializableObject
A new instance of LinearUnitAttribute as an DiGi.Core.Interfaces.ISerializableObject.

LinearUnitAttribute.From(double) Method

Converts a value from the base unit to the target linear unit.

public override double From(double value);

Parameters

value System.Double

The value in the base unit.

Returns

System.Double
The converted value after applying the factor and offset.

LinearUnitAttribute.To(double) Method

Converts a value from the target linear unit back to the base unit.

public override double To(double value);

Parameters

value System.Double

The value in the target linear unit.

Returns

System.Double
The converted value after removing the offset and dividing by the factor.

Unit Class

Represents a measurement unit associated with an enumeration value, providing access to its attributes and conversion capabilities.

public class Unit : DiGi.Core.Classes.SerializableObject, DiGi.Core.Interfaces.INamedObject, DiGi.Core.Interfaces.IObject

Inheritance System.ObjectDiGi.Core.Classes.ObjectDiGi.Core.Classes.SerializableObject → Unit

Implements DiGi.Core.Interfaces.INamedObject, DiGi.Core.Interfaces.IObject

Constructors

Unit(Enum) Constructor

Initializes a new instance of the Unit class with the specified enumeration value.

public Unit(System.Enum @enum);

Parameters

enum System.Enum

The enumeration value representing the unit.

Properties

Unit.CategoryAttribute Property

Gets the category attribute associated with this unit.

public DiGi.Unit.Classes.CategoryAttribute? CategoryAttribute { get; }

Property Value

CategoryAttribute

Unit.Enum Property

Gets the underlying enumeration value of this unit.

public System.Enum Enum { get; }

Property Value

System.Enum

Unit.Name Property

Gets the name of the unit as defined in its attributes.

public string? Name { get; }

Implements Name

Property Value

System.String

Unit.Symbol Property

Gets the symbol of the unit as defined in its attributes.

public string? Symbol { get; }

Property Value

System.String

Unit.UnitAttribute Property

Gets the unit attribute containing metadata and conversion logic for this unit.

public DiGi.Unit.Classes.UnitAttribute? UnitAttribute { get; }

Property Value

UnitAttribute

Unit.UnitCategory Property

Gets the category to which this unit belongs, or Undefined if not specified.

public DiGi.Unit.Enums.UnitCategory UnitCategory { get; }

Property Value

UnitCategory

Methods

Unit.From(double) Method

Converts a value from the unit's scale to its base representation.

public double From(double value);

Parameters

value System.Double

The value to convert.

Returns

System.Double
The converted value, or System.Double.NaN if the unit attribute is missing.

Unit.GetEnum<TEnum>() Method

Attempts to retrieve the underlying enumeration value cast to a specific enum type.

public TEnum? GetEnum<TEnum>()
    where TEnum : System.Enum;

Type parameters

TEnum

The expected enumeration type.

Returns

TEnum
The enumeration value cast to TEnum, or default if the types do not match.

Unit.IsValid() Method

Determines whether the unit is valid by checking if both its category and unit attributes are present.

public bool IsValid();

Returns

System.Boolean
True if the unit is valid; otherwise, false.

Unit.To(double) Method

Converts a base value to the unit's scale.

public double To(double value);

Parameters

value System.Double

The value to convert.

Returns

System.Double
The converted value, or System.Double.NaN if the unit attribute is missing.

Unit.TryGetEnum<TEnum>(TEnum) Method

Attempts to extract the underlying enumeration value as a specific type.

public bool TryGetEnum<TEnum>(out TEnum? @enum)
    where TEnum : System.Enum;

Type parameters

TEnum

The expected enumeration type.

Parameters

enum TEnum

When this method returns, contains the enumeration value if successful; otherwise, the default value of TEnum.

Returns

System.Boolean
True if the underlying enum is of type TEnum; otherwise, false.

Operators

Unit.explicit operator Unit(Enum) Operator

Explicitly converts an enumeration value to a Unit instance.

public static DiGi.Unit.Classes.Unit? explicit operator DiGi.Unit.Classes.Unit?(System.Enum? @enum);

Parameters

enum System.Enum

The enumeration value to convert.

Returns

Unit
A new Unit instance, or null if the provided enum is null.

Unit.explicit operator Enum(Unit) Operator

Explicitly converts a Unit instance to its underlying enumeration value.

public static System.Enum? explicit operator System.Enum?(DiGi.Unit.Classes.Unit unit);

Parameters

unit Unit

The unit instance to convert.

Returns

System.Enum
The underlying System.Enum value, or null if the unit is null.

UnitAttribute Class

Base abstract class for unit attributes, implementing DiGi.Core.Interfaces.ISerializableObject and DiGi.Core.Interfaces.INamedObject to provide standardization for units of measurement including serialization and conversion logic.

public abstract class UnitAttribute : System.Attribute, DiGi.Core.Interfaces.ISerializableObject, DiGi.Core.Interfaces.ICloneableObject<DiGi.Core.Interfaces.ISerializableObject>, DiGi.Core.Interfaces.ICloneableObject, DiGi.Core.Interfaces.IObject, DiGi.Core.Interfaces.INamedObject

Inheritance System.ObjectSystem.Attribute → UnitAttribute

Derived
FactorUnitAttribute

Implements DiGi.Core.Interfaces.ISerializableObject, DiGi.Core.Interfaces.ICloneableObject<DiGi.Core.Interfaces.ISerializableObject>, DiGi.Core.Interfaces.ICloneableObject, DiGi.Core.Interfaces.IObject, DiGi.Core.Interfaces.INamedObject

Constructors

UnitAttribute(UnitAttribute) Constructor

Initializes a new instance of the UnitAttribute class by copying values from an existing UnitAttribute.

public UnitAttribute(DiGi.Unit.Classes.UnitAttribute unit);

Parameters

unit UnitAttribute

The source unit attribute to copy from.

UnitAttribute(string, string) Constructor

Initializes a new instance of the UnitAttribute class with a specified name and symbol.

public UnitAttribute(string? name, string? symbol);

Parameters

name System.String

The descriptive name of the unit.

symbol System.String

The shorthand symbol representing the unit.

UnitAttribute(JsonObject) Constructor

Initializes a new instance of the UnitAttribute class using data from a System.Text.Json.Nodes.JsonObject.

public UnitAttribute(System.Text.Json.Nodes.JsonObject jsonObject);

Parameters

jsonObject System.Text.Json.Nodes.JsonObject

The JSON object containing the unit attribute data.

Properties

UnitAttribute.Name Property

Gets the name of the unit.

public string? Name { get; }

Implements Name

Property Value

System.String

UnitAttribute.Symbol Property

Gets the symbol of the unit.

public string? Symbol { get; }

Property Value

System.String

Methods

UnitAttribute.Clone() Method

Creates a clone of the current object.

public abstract DiGi.Core.Interfaces.ISerializableObject? Clone();

Implements Clone()

Returns

DiGi.Core.Interfaces.ISerializableObject
A cloned instance as an DiGi.Core.Interfaces.ISerializableObject, or null if cloning fails.

UnitAttribute.From(double) Method

Converts a value from this unit's scale to a base reference scale.

public abstract double From(double value);

Parameters

value System.Double

The numeric value to convert.

Returns

System.Double
The converted value in the base reference scale.

UnitAttribute.FromJsonObject(JsonObject) Method

Populates the properties of this object from a System.Text.Json.Nodes.JsonObject using DiGi.Core.Modify.FromJsonObject(DiGi.Core.Interfaces.ISerializableObject,System.Text.Json.Nodes.JsonObject).

public bool FromJsonObject(System.Text.Json.Nodes.JsonObject? jsonObject);

Parameters

jsonObject System.Text.Json.Nodes.JsonObject

The JSON object to populate from.

Implements FromJsonObject(JsonObject)

Returns

System.Boolean
True if the object was successfully populated; otherwise, false.

UnitAttribute.To(double) Method

Converts a value from a base reference scale to this unit's scale.

public abstract double To(double value);

Parameters

value System.Double

The numeric value in the base reference scale.

Returns

System.Double
The converted value in this unit's scale.

UnitAttribute.ToJsonObject() Method

Converts this object into a System.Text.Json.Nodes.JsonObject using DiGi.Core.Convert.ToJson(DiGi.Core.Interfaces.ISerializableObject).

public System.Text.Json.Nodes.JsonObject? ToJsonObject();

Implements ToJsonObject()

Returns

System.Text.Json.Nodes.JsonObject
A System.Text.Json.Nodes.JsonObject representation of the current instance.

UnitManager Class

Manages a collection of units, providing functionality to add, retrieve, and perform conversions between different unit types.

public class UnitManager

Inheritance System.Object → UnitManager

Constructors

UnitManager() Constructor

Initializes a new instance of the UnitManager class.

public UnitManager();

Methods

UnitManager.Add(Enum) Method

Adds a unit associated with the specified enum value to the manager.

public DiGi.Unit.Classes.Unit? Add(System.Enum? @enum);

Parameters

enum System.Enum

The enum value representing the unit.

Returns

Unit
The created or existing Unit object, or null if the enum is null or the unit is invalid.

UnitManager.AddRange(Assembly) Method

Scans the specified assembly for all Enum types decorated with a CategoryAttribute and adds them to the manager.

public System.Collections.Generic.List<DiGi.Unit.Classes.Unit>? AddRange(System.Reflection.Assembly? assembly);

Parameters

assembly System.Reflection.Assembly

The assembly to scan for unit enums.

Returns

System.Collections.Generic.List<Unit>
A list of all successfully added Unit objects, or null if the assembly is null.

UnitManager.AddRange(Type) Method

Adds all units defined within the specified Enum type to the manager.

public System.Collections.Generic.List<DiGi.Unit.Classes.Unit>? AddRange(System.Type? type);

Parameters

type System.Type

The Type of the enum containing the units.

Returns

System.Collections.Generic.List<Unit>
A list of successfully added Unit objects, or null if the type is null.

UnitManager.From(Enum, double, bool) Method

Converts a value from the specified unit to its base representation.

public double From(System.Enum? @enum, double value, bool update=true);

Parameters

enum System.Enum

The enum value representing the source unit.

value System.Double

The numeric value to convert.

update System.Boolean

Whether to automatically add the unit to the manager if it is not already present.

Returns

System.Double
The converted value, or double.NaN if the unit could not be retrieved.

UnitManager.GetCategoryAttribute(Enum, bool) Method

Retrieves the category attribute associated with the specified unit enum.

public DiGi.Unit.Classes.CategoryAttribute? GetCategoryAttribute(System.Enum? @enum, bool update=true);

Parameters

enum System.Enum

The enum value representing the unit.

update System.Boolean

Whether to automatically add the unit to the manager if it is not already present.

Returns

CategoryAttribute
The CategoryAttribute associated with the unit, or null if the unit could not be retrieved.

UnitManager.GetUnit(Enum, bool) Method

Retrieves the Unit object associated with the specified enum value.

public DiGi.Unit.Classes.Unit? GetUnit(System.Enum? @enum, bool update=true);

Parameters

enum System.Enum

The enum value representing the unit.

update System.Boolean

Whether to automatically add the unit to the manager if it is not already present.

Returns

Unit
The Unit object, or null if the enum is null or the unit cannot be found/created.

UnitManager.GetUnitAttribute(Enum, bool) Method

Retrieves the unit attribute associated with the specified unit enum.

public DiGi.Unit.Classes.UnitAttribute? GetUnitAttribute(System.Enum? @enum, bool update=true);

Parameters

enum System.Enum

The enum value representing the unit.

update System.Boolean

Whether to automatically add the unit to the manager if it is not already present.

Returns

UnitAttribute
The UnitAttribute associated with the unit, or null if the unit could not be retrieved.

UnitManager.To(Enum, double, bool) Method

Converts a base representation value to the specified unit.

public double To(System.Enum? @enum, double value, bool update=true);

Parameters

enum System.Enum

The enum value representing the target unit.

value System.Double

The numeric value to convert.

update System.Boolean

Whether to automatically add the unit to the manager if it is not already present.

Returns

System.Double
The converted value, or double.NaN if the unit could not be retrieved.

Clone this wiki locally