Class LogixEnum<TEnum, TValue>
- Namespace
- L5Sharp
- Assembly
- L5Sharp.dll
A base class for all logix enumeration types.
public abstract class LogixEnum<TEnum, TValue> : IEquatable<LogixEnum<TEnum, TValue>>, IComparable<LogixEnum<TEnum, TValue>> where TEnum : LogixEnum<TEnum, TValue> where TValue : IEquatable<TValue>, IComparable<TValue>
Type Parameters
TEnumThe type that is inheriting from this class.
TValueThe type of the inner value.
- Inheritance
-
LogixEnum<TEnum, TValue>
- Implements
-
IEquatable<LogixEnum<TEnum, TValue>>IComparable<LogixEnum<TEnum, TValue>>
- Derived
- Inherited Members
Remarks
This code was taken from https://github.com/ardalis/SmartEnum and modified to suit needs of this library. Wanted to remove and external dependencies and not rely on other packages. This class provided some base functionality for working with a logix enum type. This includes methods for retrieving all enums of a specified type, and parsing enums from a name or value.
Constructors
LogixEnum(string, TValue)
Creates an enumeration with the specified name and value.
protected LogixEnum(string name, TValue value)
Parameters
namestringThe common name of the enumeration option.
valueTValueThe corresponding value of the enumeration option.
Exceptions
- ArgumentNullException
nameorvalueare null.
Properties
Name
The display name of the enumeration type.
public string Name { get; }
Property Value
Value
The value of the enumeration type.
public TValue Value { get; }
Property Value
- TValue
A value of the specified enumeration type.
Methods
All()
Returns all enumeration options for the specified enumeration type.
public static IEnumerable<TEnum> All()
Returns
- IEnumerable<TEnum>
An IEnumerable<T> containing all enumeration values of the specified type.
CompareTo(LogixEnum<TEnum, TValue>)
Compares this instance to a specified LogixEnum<TEnum, TValue> and returns an indication of their relative values.
public virtual int CompareTo(LogixEnum<TEnum, TValue> other)
Parameters
otherLogixEnum<TEnum, TValue>An LogixEnum<TEnum, TValue> value to compare to this instance.
Returns
- int
A signed number indicating the relative values of this instance and
other.
Equals(LogixEnum<TEnum, TValue>?)
Returns a value indicating whether this instance is equal to a specified LogixEnum<TEnum, TValue> value.
public virtual bool Equals(LogixEnum<TEnum, TValue>? other)
Parameters
otherLogixEnum<TEnum, TValue>An LogixEnum<TEnum, TValue> value to compare to this instance.
Returns
- bool
trueifotherhas the same value as this instance; otherwise,false.
Equals(object)
Determines whether the specified object is equal to the current object.
public override bool Equals(object obj)
Parameters
objobjectThe object to compare with the current object.
Returns
FromName(string, bool)
Gets the item associated with the specified name.
public static TEnum FromName(string name, bool ignoreCase = false)
Parameters
namestringThe name of the item to get.
ignoreCasebooltrueto ignore case during the comparison; otherwise,false.
Returns
- TEnum
The item associated with the specified name. If the specified name is not found, throws a KeyNotFoundException.
Exceptions
- ArgumentException
nameisnull.- KeyNotFoundException
namedoes not exist.
- See Also
FromValue(TValue)
Gets an item associated with the specified value.
public static TEnum FromValue(TValue value)
Parameters
valueTValueThe value of the item to get.
Returns
- TEnum
The first item found that is associated with the specified value. If the specified value is not found, throws a KeyNotFoundException.
Exceptions
- KeyNotFoundException
valuedoes not exist.
- See Also
FromValue(TValue, TEnum)
Gets an item associated with the specified value.
public static TEnum FromValue(TValue value, TEnum defaultValue)
Parameters
valueTValueThe value of the item to get.
defaultValueTEnumThe value to return when item not found.
Returns
- TEnum
The first item found that is associated with the specified value. If the specified value is not found, returns
defaultValue.
- See Also
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.
TryFromName(string?, bool, out TEnum?)
Gets the item associated with the specified name.
public static bool TryFromName(string? name, bool ignoreCase, out TEnum? result)
Parameters
namestringThe name of the item to get.
ignoreCasebooltrueto ignore case during the comparison; otherwise,false.resultTEnumWhen this method returns, contains the item associated with the specified name, if the name is found; otherwise,
null. This parameter is passed uninitialized.
Returns
- bool
trueif the LogixEnum<TEnum, TValue> contains an item with the specified name; otherwise,false.
Exceptions
- ArgumentException
nameisnull.
- See Also
TryFromName(string?, out TEnum?)
Gets the item associated with the specified name.
public static bool TryFromName(string? name, out TEnum? result)
Parameters
namestringThe name of the item to get.
resultTEnumWhen this method returns, contains the item associated with the specified name, if the key is found; otherwise,
null. This parameter is passed uninitialized.
Returns
- bool
trueif the LogixEnum<TEnum, TValue> contains an item with the specified name; otherwise,false.
Exceptions
- ArgumentException
nameisnull.
- See Also
TryFromValue(TValue?, out TEnum?)
Gets an item associated with the specified value.
public static bool TryFromValue(TValue? value, out TEnum? result)
Parameters
valueTValueThe value of the item to get.
resultTEnumWhen this method returns, contains the item associated with the specified value, if the value is found; otherwise,
null. This parameter is passed uninitialized.
Returns
- bool
trueif the LogixEnum<TEnum, TValue> contains an item with the specified name; otherwise,false.
- See Also
Operators
operator ==(LogixEnum<TEnum, TValue>, LogixEnum<TEnum, TValue>)
Performs equality check on the provided LogixEnum<TEnum, TValue> types.
public static bool operator ==(LogixEnum<TEnum, TValue> left, LogixEnum<TEnum, TValue> right)
Parameters
leftLogixEnum<TEnum, TValue>An instance of the enumeration to check.
rightLogixEnum<TEnum, TValue>An instance of the enumeration to check.
Returns
- bool
trueif the types are equal; otherwise,false.
explicit operator LogixEnum<TEnum, TValue>(TValue)
public static explicit operator LogixEnum<TEnum, TValue>(TValue value)
Parameters
valueTValue
Returns
- LogixEnum<TEnum, TValue>
operator >(LogixEnum<TEnum, TValue>, LogixEnum<TEnum, TValue>)
Compares this instance to a specified LogixEnum<TEnum, TValue> and returns an indication if
left is greater than right.
public static bool operator >(LogixEnum<TEnum, TValue> left, LogixEnum<TEnum, TValue> right)
Parameters
leftLogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
rightLogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
Returns
- bool
trueif left is greater thanright.
operator >=(LogixEnum<TEnum, TValue>, LogixEnum<TEnum, TValue>)
Compares this instance to a specified LogixEnum<TEnum, TValue> and returns an indication if
left is greater than or equal to right.
public static bool operator >=(LogixEnum<TEnum, TValue> left, LogixEnum<TEnum, TValue> right)
Parameters
leftLogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
rightLogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
Returns
- bool
trueif left is greater than or equal toright.
implicit operator TValue(LogixEnum<TEnum, TValue>)
Implicitly converts the provided LogixEnum<TEnum, TValue> to the underlying value.
public static implicit operator TValue(LogixEnum<TEnum, TValue> logixEnum)
Parameters
logixEnumLogixEnum<TEnum, TValue>The enumeration type.
Returns
- TValue
A value representing the
operator !=(LogixEnum<TEnum, TValue>, LogixEnum<TEnum, TValue>)
Performs equality check on the provided LogixEnum<TEnum, TValue> types.
public static bool operator !=(LogixEnum<TEnum, TValue> left, LogixEnum<TEnum, TValue> right)
Parameters
leftLogixEnum<TEnum, TValue>An instance of the enumeration to check.
rightLogixEnum<TEnum, TValue>An instance of the enumeration to check.
Returns
- bool
trueif the types are NOT equal; otherwise,false.
operator <(LogixEnum<TEnum, TValue>, LogixEnum<TEnum, TValue>)
Compares this instance to a specified LogixEnum<TEnum, TValue> and returns an indication if
left is less than right.
public static bool operator <(LogixEnum<TEnum, TValue> left, LogixEnum<TEnum, TValue> right)
Parameters
leftLogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
rightLogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
Returns
- bool
trueif left is less thanright.
operator <=(LogixEnum<TEnum, TValue>, LogixEnum<TEnum, TValue>)
Compares this instance to a specified LogixEnum<TEnum, TValue> and returns an indication if
left is less than or equal to right.
public static bool operator <=(LogixEnum<TEnum, TValue> left, LogixEnum<TEnum, TValue> right)
Parameters
leftLogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
rightLogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
Returns
- bool
trueif left is less than or equal toright.