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.
converterFunc<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
converterFunc<TFrom, TTo>The converter function to add.
Type Parameters
TFromThe source type to convert from.
TToThe 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
actionActionThe 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
funcFunc<T>The function to wrap.
Returns
- Func<T>
A function that executes using the invariant culture.
Type Parameters
TThe 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
bytesbyte[]The byte array to modify.
lengthintThe number of bytes to consider.
isLittleEndianboolSpecifies the desired endianness.
posintThe 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
cultureInfoCultureInfoThe culture to use during execution.
actionActionThe 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
cultureInfoCultureInfoThe culture to use during execution.
funcFunc<T>The function to execute.
Returns
- T
The result of the function execution.
Type Parameters
TThe 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
endPointEndPointThe 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
endPointEndPointThe 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
TFromThe source type to convert from.
TToThe 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
decimalNumberlongThe number to convert.
radixintThe 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
valueobjectThe 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
typeTypeThe 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
aliasstringThe 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
fromTFromThe value to convert.
Returns
- TTo
The converted value.
Type Parameters
TFromThe source type to convert from.
TToThe destination type to convert to.