Class Converter
The converter class.
public static class Converter
- Inheritance
-
Converter
- Inherited Members
Methods
AddCSharpAlias(Type, string)
Adds a C# language alias for a type.
public static void AddCSharpAlias(Type type, string alias)
Parameters
Exceptions
- ArgumentNullException
Thrown when type or alias is null.
- ArgumentOutOfRangeException
Thrown when alias or type already exists.
AddTypedConverter((Type, Type), Func<object, object>)
Adds a typed converter function for converting between specified types.
public static void AddTypedConverter((Type, Type) key, Func<object, object> converter)
Parameters
key
(Type, Type)Tuple containing source and destination types.
converter
Func<object, object>The converter function to add.
Exceptions
- ArgumentNullException
Thrown when converter is null.
AddTypedConverter<TFrom, TTo>(Func<TFrom, TTo>)
Adds a typed converter function for converting from TFrom to TTo type.
public static void AddTypedConverter<TFrom, TTo>(Func<TFrom, TTo> converter)
Parameters
converter
Func<TFrom, TTo>The converter function to add.
Type Parameters
TFrom
The source type to convert from.
TTo
The destination type to convert to.
Exceptions
- ArgumentNullException
Thrown when converter is null.
AsInvariant(Action)
Wraps an action to execute using the invariant culture.
public static Action AsInvariant(this Action action)
Parameters
action
ActionThe action to wrap.
Returns
- Action
An action that executes using the invariant culture.
Exceptions
- ArgumentNullException
Thrown when action is null.
AsInvariant<T>(Func<T>)
Wraps a function to execute using the invariant culture.
public static Func<T> AsInvariant<T>(this Func<T> func)
Parameters
func
Func<T>The function to wrap.
Returns
- Func<T>
A function that executes using the invariant culture.
Type Parameters
T
The return type of the function.
Exceptions
- ArgumentNullException
Thrown when func is null.
ChangeOrder(byte[], int, bool, int)
Reverses the byte order if the specified endianness differs from the system endianness.
public static byte[] ChangeOrder(this byte[] bytes, int length, bool isLittleEndian, int pos = 0)
Parameters
bytes
byte[]The byte array to modify.
length
intThe number of bytes to consider.
isLittleEndian
boolSpecifies the desired endianness.
pos
intThe starting position within the array.
Returns
- byte[]
The modified byte array.
DoInCulture(CultureInfo, Action)
Executes an action using the specified culture.
public static void DoInCulture(this CultureInfo cultureInfo, Action action)
Parameters
cultureInfo
CultureInfoThe culture to use during execution.
action
ActionThe action to execute.
Exceptions
- ArgumentNullException
Thrown when cultureInfo or action is null.
DoInCulture<T>(CultureInfo, Func<T>)
Executes a function using the specified culture.
public static T DoInCulture<T>(this CultureInfo cultureInfo, Func<T> func)
Parameters
cultureInfo
CultureInfoThe culture to use during execution.
func
Func<T>The function to execute.
Returns
- T
The result of the function execution.
Type Parameters
T
The return type of the function.
Exceptions
- ArgumentNullException
Thrown when cultureInfo or func is null.
GetHost(EndPoint)
Retrieves the host from the specified endpoint.
public static string GetHost(this EndPoint endPoint)
Parameters
endPoint
EndPointThe endpoint to extract the host from.
Returns
- string
The host string.
Exceptions
- ArgumentNullException
Thrown if endPoint is null.
- InvalidOperationException
Thrown if the endpoint type is unknown.
GetPort(EndPoint)
Retrieves the port number from the specified endpoint.
public static int GetPort(this EndPoint endPoint)
Parameters
endPoint
EndPointThe endpoint to extract the port from.
Returns
- int
The port number.
Exceptions
- ArgumentNullException
Thrown if endPoint is null.
- InvalidOperationException
Thrown if the endpoint type is unknown.
GetTypedConverter(Type, Type)
Gets the typed converter function for converting between specified types.
public static Func<object, object> GetTypedConverter(Type from, Type to)
Parameters
Returns
GetTypedConverter<TFrom, TTo>()
Gets the typed converter function for converting between specified types.
public static Func<TFrom, TTo> GetTypedConverter<TFrom, TTo>()
Returns
- Func<TFrom, TTo>
A converter function for the specified types.
Type Parameters
TFrom
The source type to convert from.
TTo
The destination type to convert to.
SetHost(EndPoint, string)
Sets the host on the specified endpoint.
public static EndPoint SetHost(this EndPoint endPoint, string host)
Parameters
Returns
- EndPoint
The modified endpoint.
Exceptions
- ArgumentNullException
Thrown if endPoint is null.
- InvalidOperationException
Thrown if the endpoint type is unknown.
SetPort(EndPoint, int)
Sets the port number on the specified endpoint.
public static EndPoint SetPort(this EndPoint endPoint, int port)
Parameters
Returns
- EndPoint
The modified endpoint.
Exceptions
- ArgumentNullException
Thrown if endPoint is null.
- InvalidOperationException
Thrown if the endpoint type is unknown.
To(object, Type)
Convert value into a instance of destinationType
.
public static object To(this object value, Type destinationType)
Parameters
Returns
- object
Converted object.
ToRadix(long, int)
Converts the given decimal number to the numeral system with the specified radix (in the range [2, 36]).
public static string ToRadix(this long decimalNumber, int radix)
Parameters
decimalNumber
longThe number to convert.
radix
intThe radix of the destination numeral system (in the range [2, 36]).
Returns
To<T>(object)
Convert value into a instance of destinationType.
public static T To<T>(this object value)
Parameters
value
objectThe value.
Returns
- T
Converted object.
Type Parameters
T
TryGetCSharpAlias(Type)
Attempts to get the C# language alias for a type.
public static string TryGetCSharpAlias(this Type type)
Parameters
type
TypeThe type to get the alias for.
Returns
- string
The C# alias if found; otherwise null.
TryGetTypeByCSharpAlias(string)
Attempts to get a type by its C# language alias.
public static Type TryGetTypeByCSharpAlias(this string alias)
Parameters
alias
stringThe C# alias to look up.
Returns
- Type
The corresponding type if found; otherwise null.
TypedTo<TFrom, TTo>(TFrom)
Converts the value from source type to destination type using registered type converters.
public static TTo TypedTo<TFrom, TTo>(this TFrom from)
Parameters
from
TFromThe value to convert.
Returns
- TTo
The converted value.
Type Parameters
TFrom
The source type to convert from.
TTo
The destination type to convert to.