• Basics
  • Abyss
  • Web APIs
  • C# & Razor
  • .net API
  • JS & TS API

    Show / Hide Table of Contents

    Interface IConvertService

    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 IConvertService
    Remarks

    New in v12.05

    Properties

    | Improve this Doc View Source

    Json

    Sub-Service to convert JSON

    Declaration
    IJsonService Json { get; }
    Property Value
    Type Description
    IJsonService
    | Improve this Doc View Source

    OptimizeBoolean

    If set to true, will treat a number like 2 or -1 and strings like "2" as true. If set to false, only 1 will be true, other numbers will be false.

    Declaration
    bool OptimizeBoolean { get; }
    Property Value
    Type Description
    System.Boolean
    | Improve this Doc View Source

    OptimizeNumbers

    If set to true (default) will optimize converting numbers. For example, a string like "4.2" will properly convert to an int of 2. If set to false, this optimization doesn't happen and a string "4.2" would result in a 0 int

    Declaration
    bool OptimizeNumbers { get; }
    Property Value
    Type Description
    System.Boolean

    Methods

    | Improve this Doc View Source

    ForCode(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 or false (not True or False)
    • 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
    System.Object value
    Returns
    Type Description
    System.String
    | Improve this Doc View Source

    ForCode(Object, String)

    Same as ForCode(Object), but with fallback, in case the conversion fails.

    Declaration
    string ForCode(object value, string fallback = null)
    Parameters
    Type Name Description
    System.Object value
    System.String fallback
    Returns
    Type Description
    System.String
    | Improve this Doc View Source

    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
    System.Object value
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T
    | Improve this Doc View Source

    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(&quot;5&quot;, 100); // will return 5
    var c2 = Convert.To(&quot;&quot;, 100);  // will return 100
    
    Declaration
    T To<T>(object value, string paramsMustBeNamed = "Rule: All params must be named (https://r.2sxc.org/named-params)", T fallback = null)
    Parameters
    Type Name Description
    System.Object value
    System.String paramsMustBeNamed

    requires that all params must be named, like fallback: 27

    T fallback

    The value used if conversion fails.

    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T
    | Improve this Doc View Source

    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
    System.Object value
    Returns
    Type Description
    System.Boolean
    | Improve this Doc View Source

    ToBool(Object, Boolean)

    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, bool fallback = false)
    Parameters
    Type Name Description
    System.Object value
    System.Boolean fallback
    Returns
    Type Description
    System.Boolean
    | Improve this Doc View Source

    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
    System.Object value
    Returns
    Type Description
    System.Decimal
    | Improve this Doc View Source

    ToDecimal(Object, 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, decimal fallback = 0M)
    Parameters
    Type Name Description
    System.Object value
    System.Decimal fallback
    Returns
    Type Description
    System.Decimal
    | Improve this Doc View Source

    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
    System.Object value
    Returns
    Type Description
    System.Double
    | Improve this Doc View Source

    ToDouble(Object, 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, double fallback = 0)
    Parameters
    Type Name Description
    System.Object value
    System.Double fallback
    Returns
    Type Description
    System.Double
    | Improve this Doc View Source

    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
    System.Object value
    Returns
    Type Description
    System.Single
    | Improve this Doc View Source

    ToFloat(Object, Single)

    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, float fallback = 0F)
    Parameters
    Type Name Description
    System.Object value
    System.Single fallback
    Returns
    Type Description
    System.Single
    | Improve this Doc View Source

    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
    System.Object value
    Returns
    Type Description
    System.Guid
    | Improve this Doc View Source

    ToGuid(Object, 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, Guid fallback = default(Guid))
    Parameters
    Type Name Description
    System.Object value
    System.Guid fallback
    Returns
    Type Description
    System.Guid
    | Improve this Doc View Source

    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
    System.Object value
    Returns
    Type Description
    System.Int32
    | Improve this Doc View Source

    ToInt(Object, Int32)

    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, int fallback = 0)
    Parameters
    Type Name Description
    System.Object value
    System.Int32 fallback
    Returns
    Type Description
    System.Int32
    | Improve this Doc View Source

    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
    System.Object value
    Returns
    Type Description
    System.String
    | Improve this Doc View Source

    ToString(Object, String, String, Boolean)

    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 fallback = null, string paramsMustBeNamed = "Rule: All params must be named (https://r.2sxc.org/named-params)", bool fallbackOnNull = true)
    Parameters
    Type Name Description
    System.Object value
    System.String fallback
    System.String paramsMustBeNamed
    System.Boolean fallbackOnNull
    Returns
    Type Description
    System.String
    • Improve this Doc
    • View Source
    Back to top Generated by DocFX