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
TEnum
The type that is inheriting from this class.
TValue
The 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
name
stringThe common name of the enumeration option.
value
TValueThe corresponding value of the enumeration option.
Exceptions
- ArgumentNullException
name
orvalue
are 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
other
LogixEnum<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
other
LogixEnum<TEnum, TValue>An LogixEnum<TEnum, TValue> value to compare to this instance.
Returns
- bool
true
ifother
has 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
obj
objectThe 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
name
stringThe name of the item to get.
ignoreCase
booltrue
to 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
name
isnull
.- KeyNotFoundException
name
does not exist.
- See Also
FromValue(TValue)
Gets an item associated with the specified value.
public static TEnum FromValue(TValue value)
Parameters
value
TValueThe 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
value
does not exist.
- See Also
FromValue(TValue, TEnum)
Gets an item associated with the specified value.
public static TEnum FromValue(TValue value, TEnum defaultValue)
Parameters
value
TValueThe value of the item to get.
defaultValue
TEnumThe 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
name
stringThe name of the item to get.
ignoreCase
booltrue
to ignore case during the comparison; otherwise,false
.result
TEnumWhen 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
true
if the LogixEnum<TEnum, TValue> contains an item with the specified name; otherwise,false
.
Exceptions
- ArgumentException
name
isnull
.
- See Also
TryFromName(string?, out TEnum?)
Gets the item associated with the specified name.
public static bool TryFromName(string? name, out TEnum? result)
Parameters
name
stringThe name of the item to get.
result
TEnumWhen 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
true
if the LogixEnum<TEnum, TValue> contains an item with the specified name; otherwise,false
.
Exceptions
- ArgumentException
name
isnull
.
- See Also
TryFromValue(TValue?, out TEnum?)
Gets an item associated with the specified value.
public static bool TryFromValue(TValue? value, out TEnum? result)
Parameters
value
TValueThe value of the item to get.
result
TEnumWhen 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
true
if 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
left
LogixEnum<TEnum, TValue>An instance of the enumeration to check.
right
LogixEnum<TEnum, TValue>An instance of the enumeration to check.
Returns
- bool
true
if the types are equal; otherwise,false
.
explicit operator LogixEnum<TEnum, TValue>(TValue)
public static explicit operator LogixEnum<TEnum, TValue>(TValue value)
Parameters
value
TValue
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
left
LogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
right
LogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
Returns
- bool
true
if 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
left
LogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
right
LogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
Returns
- bool
true
if 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
logixEnum
LogixEnum<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
left
LogixEnum<TEnum, TValue>An instance of the enumeration to check.
right
LogixEnum<TEnum, TValue>An instance of the enumeration to check.
Returns
- bool
true
if 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
left
LogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
right
LogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
Returns
- bool
true
if 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
left
LogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
right
LogixEnum<TEnum, TValue>An instance of LogixEnum<TEnum, TValue> to compare.
Returns
- bool
true
if left is less than or equal toright
.