Table of Contents

Class OptionConverter

Namespace
log4net.Util
Assembly
log4net.dll

A convenience class to convert property values to specific types.

public sealed class OptionConverter
Inheritance
OptionConverter
Inherited Members

Remarks

Utility functions for converting types and parsing values.

Methods

CanConvertTypeTo(Type, Type)

Checks if there is an appropriate type conversion from the source type to the target type.

public static bool CanConvertTypeTo(Type sourceType, Type targetType)

Parameters

sourceType Type

The type to convert from.

targetType Type

The type to convert to.

Returns

bool

true if there is a conversion from the source type to the target type.

Remarks

Checks if there is an appropriate type conversion from the source type to the target type.

ConvertStringTo(Type, string)

Converts a string to an object.

public static object ConvertStringTo(Type target, string txt)

Parameters

target Type

The target type to convert to.

txt string

The string to convert to an object.

Returns

object

The object converted from a string or null when the conversion failed.

Remarks

Converts a string to an object. Uses the converter registry to try to convert the string value into the specified target type.

ConvertTypeTo(object, Type)

Converts an object to the target type.

public static object ConvertTypeTo(object sourceInstance, Type targetType)

Parameters

sourceInstance object

The object to convert to the target type.

targetType Type

The type to convert to.

Returns

object

The converted object.

Remarks

Converts an object to the target type.

InstantiateByClassName(string, Type, object)

Instantiates an object given a class name.

public static object InstantiateByClassName(string className, Type superClass, object defaultValue)

Parameters

className string

The fully qualified class name of the object to instantiate.

superClass Type

The class to which the new object should belong.

defaultValue object

The object to return in case of non-fulfillment.

Returns

object

An instance of the className or defaultValue if the object could not be instantiated.

Remarks

Checks that the className is a subclass of superClass. If that test fails or the object could not be instantiated, then defaultValue is returned.

SubstituteVariables(string, IDictionary)

Performs variable substitution in string value from the values of keys found in props.

public static string SubstituteVariables(string value, IDictionary props)

Parameters

value string

The string on which variable substitution is performed.

props IDictionary

The dictionary to use to lookup variables.

Returns

string

The result of the substitutions.

Remarks

The variable substitution delimiters are ${ and }.

For example, if props contains key=value, then the call

string s = OptionConverter.SubstituteVariables("Value of key is ${key}.");

will set the variable s to "Value of key is value.".

If no value could be found for the specified key, then substitution defaults to an empty string.

For example, if system properties contains no value for the key "nonExistentKey", then the call

string s = OptionConverter.SubstituteVariables("Value of nonExistentKey is [${nonExistentKey}]");

will set s to "Value of nonExistentKey is []".

An Exception is thrown if value contains a start delimiter "${" which is not balanced by a stop delimiter "}".

ToBoolean(string, bool)

Converts a string to a bool value.

public static bool ToBoolean(string argValue, bool defaultValue)

Parameters

argValue string

String to convert.

defaultValue bool

The default value.

Returns

bool

The bool value of argValue.

Remarks

If argValue is "true", then true is returned. If argValue is "false", then false is returned. Otherwise, defaultValue is returned.

ToFileSize(string, long)

Parses a file size into a number.

public static long ToFileSize(string argValue, long defaultValue)

Parameters

argValue string

String to parse.

defaultValue long

The default value.

Returns

long

The long value of argValue.

Remarks

Parses a file size of the form: number[KB|MB|GB] into a long value. It is scaled with the appropriate multiplier.

defaultValue is returned when argValue cannot be converted to a long value.