Class OptionConverter
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
Returns
- bool
trueif 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
Returns
- object
The object converted from a string or
nullwhen 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
sourceInstanceobjectThe object to convert to the target type.
targetTypeTypeThe 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
classNamestringThe fully qualified class name of the object to instantiate.
superClassTypeThe class to which the new object should belong.
defaultValueobjectThe object to return in case of non-fulfillment.
Returns
- object
An instance of the
classNameordefaultValueif 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
valuestringThe string on which variable substitution is performed.
propsIDictionaryThe 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
Returns
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
Returns
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.