Table of Contents

Class Converter

Namespace
Ecng.Common
Assembly
Ecng.Common.dll

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

type Type

The type to add an alias for.

alias string

The C# alias to associate with the type.

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 Action

The 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 int

The number of bytes to consider.

isLittleEndian bool

Specifies the desired endianness.

pos int

The 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 CultureInfo

The culture to use during execution.

action Action

The 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 CultureInfo

The 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 EndPoint

The 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 EndPoint

The 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

from Type

The source type to convert from.

to Type

The destination type to convert to.

Returns

Func<object, object>

A converter function for the specified types.

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

endPoint EndPoint

The endpoint to modify.

host string

The new host name or IP address.

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

endPoint EndPoint

The endpoint to modify.

port int

The new port number.

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

value object

The value.

destinationType Type

Type of the destination.

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 long

The number to convert.

radix int

The radix of the destination numeral system (in the range [2, 36]).

Returns

string

To<T>(object)

Convert value into a instance of destinationType.

public static T To<T>(this object value)

Parameters

value object

The 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 Type

The 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 string

The 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 TFrom

The value to convert.

Returns

TTo

The converted value.

Type Parameters

TFrom

The source type to convert from.

TTo

The destination type to convert to.