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
Namespace: ToSic.Sxc.Services
Assembly: ToSic.Sxc.dll
Syntax
[PublicApi]
public interface IConvertService16
Remarks
- New in v16.03
- Difference to IConvertService is that the param
fallback
must always be named
Properties
| Improve this Doc View SourceJson
Sub-Service to convert JSON
Declaration
IJsonService Json { get; }
Property Value
Type | Description |
---|---|
IJsonService |
Methods
| Improve this Doc View SourceForCode(object)
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
Declaration
string ForCode(object value)
Parameters
Type | Name | Description |
---|---|---|
object | value |
Returns
Type | Description |
---|---|
string |
ForCode(object, string, string)
Same as ForCode(object), but with fallback, in case the conversion fails.
Declaration
string ForCode(object value, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", string fallback = null)
Parameters
Type | Name | Description |
---|---|---|
object | value | value to convert |
string | noParamOrder | |
string | fallback | The value used if conversion fails. |
Returns
Type | Description |
---|---|
string |
ToBool(object)
Convert any object safely to bool. This does the same as To<T>(object) but this is easier to type in Razor.
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(...)
Declaration
bool ToBool(object value)
Parameters
Type | Name | Description |
---|---|---|
object | value |
Returns
Type | Description |
---|---|
bool |
ToBool(object, string, 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(...)
Declaration
bool ToBool(object value, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", bool fallback = false)
Parameters
Type | Name | Description |
---|---|---|
object | value | value to convert |
string | noParamOrder | |
bool | fallback | The value used if conversion fails. |
Returns
Type | Description |
---|---|
bool |
ToDecimal(object)
Convert any object safely to decimal. This does the same as To<T>(object) but this is easier to type in Razor.
Declaration
decimal ToDecimal(object value)
Parameters
Type | Name | Description |
---|---|---|
object | value |
Returns
Type | Description |
---|---|
decimal |
ToDecimal(object, string, decimal)
Convert any object safely to decimal, or if that fails, return the fallback value. This does the same as To<T>(object) but this is easier to type in Razor.
Declaration
decimal ToDecimal(object value, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", decimal fallback = 0)
Parameters
Type | Name | Description |
---|---|---|
object | value | value to convert |
string | noParamOrder | |
decimal | fallback | The value used if conversion fails. |
Returns
Type | Description |
---|---|
decimal |
ToDouble(object)
Convert any object safely to double. This does the same as To<T>(object) but this is easier to type in Razor.
Declaration
double ToDouble(object value)
Parameters
Type | Name | Description |
---|---|---|
object | value |
Returns
Type | Description |
---|---|
double |
ToDouble(object, string, double)
Convert any object safely to double, or if that fails, return the fallback value. This does the same as To<T>(object) but this is easier to type in Razor.
Declaration
double ToDouble(object value, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", double fallback = 0)
Parameters
Type | Name | Description |
---|---|---|
object | value | value to convert |
string | noParamOrder | |
double | fallback | The value used if conversion fails. |
Returns
Type | Description |
---|---|
double |
ToFloat(object)
Convert any object safely to float. This does the same as To<T>(object) 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(...)
Declaration
float ToFloat(object value)
Parameters
Type | Name | Description |
---|---|---|
object | value |
Returns
Type | Description |
---|---|
float |
ToFloat(object, string, float)
Convert any object safely to float, or if that fails, return the fallback value. This does the same as To<T>(object) 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(...)
Declaration
float ToFloat(object value, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", float fallback = 0)
Parameters
Type | Name | Description |
---|---|---|
object | value | value to convert |
string | noParamOrder | |
float | fallback | The value used if conversion fails. |
Returns
Type | Description |
---|---|
float |
ToGuid(object)
Convert any object safely to a Guid This does the same as To<T>(object) but this is easier to type in Razor.
Declaration
Guid ToGuid(object value)
Parameters
Type | Name | Description |
---|---|---|
object | value |
Returns
Type | Description |
---|---|
Guid |
ToGuid(object, string, Guid)
Convert any object safely to standard guid, or if that fails, return the fallback value. This does the same as To<T>(object) but this is easier to type in Razor.
Declaration
Guid ToGuid(object value, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", Guid fallback = default)
Parameters
Type | Name | Description |
---|---|---|
object | value | value to convert |
string | noParamOrder | |
Guid | fallback | The value used if conversion fails. |
Returns
Type | Description |
---|---|
Guid |
ToInt(object)
Convert any object safely to standard int. This does the same as To<T>(object) but this is easier to type in Razor.
Declaration
int ToInt(object value)
Parameters
Type | Name | Description |
---|---|---|
object | value |
Returns
Type | Description |
---|---|
int |
ToInt(object, string, int)
Convert any object safely to standard int, or if that fails, return the fallback value. This does the same as To<T>(object) but this is easier to type in Razor.
Declaration
int ToInt(object value, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", int fallback = 0)
Parameters
Type | Name | Description |
---|---|---|
object | value | value to convert |
string | noParamOrder | |
int | fallback | The value used if conversion fails. |
Returns
Type | Description |
---|---|
int |
ToString(object)
Convert any object safely to string. This does the same as To<T>(object) but this is easier to type in Razor.
Declaration
string ToString(object value)
Parameters
Type | Name | Description |
---|---|---|
object | value |
Returns
Type | Description |
---|---|
string |
ToString(object, string, 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, string, 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.
Declaration
string ToString(object value, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", string fallback = null, bool fallbackOnNull = true)
Parameters
Type | Name | Description |
---|---|---|
object | value | The value to convert |
string | noParamOrder | |
string | fallback | Fallback in case conversion fails or result is null |
bool | fallbackOnNull | Determine that nulls should also fallback, default is |
Returns
Type | Description |
---|---|
string |
To<T>(object)
Convert any object safely to the desired type T.
If conversion fails, it will return default(T)
, which is 0 for most numbers, false
for boolean or null
for strings or objects.
Declaration
T To<T>(object value)
Parameters
Type | Name | Description |
---|---|---|
object | value |
Returns
Type | Description |
---|---|
T |
Type Parameters
Name | Description |
---|---|
T |
To<T>(object, string, T)
Convert any object safely to the desired type T.
If conversion fails, it will return the fallback
parameter as given.
Since the fallback is typed, you can usually call this method without specifying T explicitly, so this should work:
var c1 = Convert.To("5", 100); // will return 5
var c2 = Convert.To("", 100); // will return 100
Declaration
T To<T>(object value, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", T fallback = default)
Parameters
Type | Name | Description |
---|---|---|
object | value | value to convert |
string | noParamOrder | |
T | fallback | The value used if conversion fails. |
Returns
Type | Description |
---|---|
T |
Type Parameters
Name | Description |
---|---|
T |