Interface IConvertService16
Conversion helper for things which are very common in web-code like Razor and WebAPIs. It's mainly a safe conversion from anything to a target-type.
Some special things it does:
- Strings like "4.2" reliably get converted to int 4 which would otherwise return 0
- Numbers like 42 reliably converts to bool true which would otherwise return false
- Numbers like 42.5 reliably convert to strings "42.5" instead of "42,5" in certain cultures
[PublicApi]
public interface IConvertService16
Remarks
- New in v16.03
- Difference to IConvertService is that the param
fallback
must always be named
Properties
Json
Sub-Service to convert JSON
IJsonService Json { get; }
Property Value
Methods
ForCode(object, NoParamOrder, string)
Convert any object safely to string to put into source code like HTML-attributes, inline-JavaScript or similar. This is usually used to ensure numbers, booleans and dates are in a format which works. Especially useful when giving data to a JavaScript, Json-Fragment or an Html Attribute.
- booleans will be
true
orfalse
(notTrue
orFalse
) - numbers will have a . notation and never a comma (like in de-DE cultures)
- dates will convert to ISO format without time zone
Optionally also allows a fallback
to use instead of the defaults above.
string ForCode(object value, NoParamOrder noParamOrder = default, string fallback = null)
Parameters
value
objectvalue to convert
noParamOrder
NoParamOrderfallback
stringThe value used if conversion fails. Defaults to
null
.
Returns
ToBool(object, NoParamOrder, bool)
Convert any object safely to bool, or if that fails, return the fallback value.
Note that it's called ToBool, not ToBoolean, because the core type is also called bool, not boolean. This is different from System.Convert.ToBoolean(...)
bool ToBool(object value, NoParamOrder noParamOrder = default, bool fallback = false)
Parameters
value
objectvalue to convert
noParamOrder
NoParamOrderfallback
boolThe value used if conversion fails. Defaults to
false
.
Returns
ToDecimal(object, NoParamOrder, decimal)
Convert any object safely to decimal, or if that fails, return the fallback value. This does the same as To<T>(object, NoParamOrder, T) but this is easier to type in Razor.
decimal ToDecimal(object value, NoParamOrder noParamOrder = default, decimal fallback = 0)
Parameters
value
objectvalue to convert
noParamOrder
NoParamOrderfallback
decimalThe value used if conversion fails. Defaults to
0
.
Returns
ToDouble(object, NoParamOrder, double)
Convert any object safely to double, or if that fails, return the fallback value. This does the same as To<T>(object, NoParamOrder, T) but this is easier to type in Razor.
double ToDouble(object value, NoParamOrder noParamOrder = default, double fallback = 0)
Parameters
value
objectvalue to convert
noParamOrder
NoParamOrderfallback
doubleThe value used if conversion fails. Defaults to
0
.
Returns
ToFloat(object, NoParamOrder, float)
Convert any object safely to float, or if that fails, return the fallback value. This does the same as To<T>(object, NoParamOrder, T) but this is easier to type in Razor.
Note that it's called ToFloat, not ToSingle, because the core type is also called float, not single. This is different from System.Convert.ToSingle(...)
float ToFloat(object value, NoParamOrder noParamOrder = default, float fallback = 0)
Parameters
value
objectvalue to convert
noParamOrder
NoParamOrderfallback
floatThe value used if conversion fails. Defaults to
0
.
Returns
ToGuid(object, NoParamOrder, Guid)
Convert any object safely to standard guid, or if that fails, return the fallback value. This does the same as To<T>(object, NoParamOrder, T) but this is easier to type in Razor.
Guid ToGuid(object value, NoParamOrder noParamOrder = default, Guid fallback = default)
Parameters
value
objectvalue to convert
noParamOrder
NoParamOrderfallback
GuidThe value used if conversion fails. Defaults to
Guid.Empty
.
Returns
ToInt(object, NoParamOrder, int)
Convert any object safely to standard int, or if that fails, return the fallback value. This does the same as To<T>(object, NoParamOrder, T) but this is easier to type in Razor.
int ToInt(object value, NoParamOrder noParamOrder = default, int fallback = 0)
Parameters
value
objectvalue to convert
noParamOrder
NoParamOrderfallback
intThe value used if conversion fails. Defaults to
0
.
Returns
ToString(object, NoParamOrder, string, bool)
Convert any object safely to string - or if that fails, return the fallback value.
This does NOT do the same as To<T>(object, NoParamOrder, T). In the standard implementation would only give you the fallback, if conversion failed. But this ToString will also give you the fallback, if the result is null.
string ToString(object value, NoParamOrder noParamOrder = default, string fallback = null, bool fallbackOnNull = true)
Parameters
value
objectThe value to convert
noParamOrder
NoParamOrderfallback
stringFallback in case conversion fails or result is null. Defaults to
null
.fallbackOnNull
boolDetermine that nulls should also fallback, default is
true
Returns
To<T>(object, NoParamOrder, T)
Convert any object safely to the desired type.
If conversion fails, it will return the fallback
parameter as given, or default(T)
.
Since the fallback is typed, you can usually call this method without specifying T explicitly, so this should work:
var c1 = Convert.To("5", fallback: 100); // will return 5
var c2 = Convert.To("", fallback: 100); // will return 100
var c1 = Convert.To(""); // will return 0
T To<T>(object value, NoParamOrder noParamOrder = default, T fallback = default)
Parameters
value
objectvalue to convert
noParamOrder
NoParamOrderfallback
TThe value used if conversion fails. If not specified, will use
default(T)
Returns
- T
Type Parameters
T