Table of Contents

Interface ITyped

Namespace
ToSic.Sxc.Data
Assembly
ToSic.Sxc.dll

This describes objects which usually wrap other objects to provide strictly typed access to properties. have typed Methods to read properties like .String(propName).

It's usually the result of a AsTyped(something) or AsItem(...) command.

It's meant to help Razor etc. access unknown or dynamic objects in a typed way.

[PublicApi]
[JsonConverter(typeof(DynamicJsonConverter))]
public interface ITyped

Remarks

New in 16.02.

Methods

Attribute(string, NoParamOrder, string, bool?)

Return a value as a raw HTML string for using inside an attribute. Usage eg. title='@item.Attribute("Title")' It will do a few things such as:

  1. Ensure dates are in the ISO format
  2. Ensure numbers are in a neutral format such as 14.27 and never 14,27
  3. Html encode any characters which would cause trouble such as quotes
IRawHtmlString Attribute(string name, NoParamOrder noParamOrder = default, string fallback = null, bool? required = null)

Parameters

name string

Name of the property

noParamOrder NoParamOrder

see Convention: Named Parameters

fallback string

Value to use if the property specified by name doesn't exist

required bool?

throw error if name doesn't exist, see Convention: Property Required Name Checks

Returns

IRawHtmlString

Bool(string, NoParamOrder, bool, bool?)

Get a property and return the value as a bool. If conversion fails, will return default false or what is specified in the fallback.

bool Bool(string name, NoParamOrder noParamOrder = default, bool fallback = false, bool? required = null)

Parameters

name string

property name

noParamOrder NoParamOrder

see Convention: Named Parameters

fallback bool

optional fallback if conversion fails

required bool?

throw error if the name doesn't exist, see Convention: Property Required Name Checks

Returns

bool

Value as bool

ContainsKey(string)

Check if this typed object has a property of this specified name. It's case-insensitive.

bool ContainsKey(string name)

Parameters

name string

the name like Image; some objects also support path to sub-property like Author.Name

Returns

bool

Remarks

Adding in 16.03 (WIP)

DateTime(string, NoParamOrder, DateTime, bool?)

Get a property and return the value as a DateTime. If conversion fails, will return default 0001-01-01 or what is specified in the fallback.

DateTime DateTime(string name, NoParamOrder noParamOrder = default, DateTime fallback = default, bool? required = null)

Parameters

name string

property name

noParamOrder NoParamOrder

see Convention: Named Parameters

fallback DateTime

optional fallback if conversion fails

required bool?

throw error if the name doesn't exist, see Convention: Property Required Name Checks

Returns

DateTime

Value as DateTime

Decimal(string, NoParamOrder, decimal, bool?)

Get a property and return the value as a decimal. If conversion fails, will return default 0 or what is specified in the fallback.

decimal Decimal(string name, NoParamOrder noParamOrder = default, decimal fallback = 0, bool? required = null)

Parameters

name string

property name

noParamOrder NoParamOrder

see Convention: Named Parameters

fallback decimal

optional fallback if conversion fails

required bool?

throw error if the name doesn't exist, see Convention: Property Required Name Checks

Returns

decimal

Value as decimal

Double(string, NoParamOrder, double, bool?)

Get a property and return the value as a double. If conversion fails, will return default 0 or what is specified in the fallback.

double Double(string name, NoParamOrder noParamOrder = default, double fallback = 0, bool? required = null)

Parameters

name string

property name

noParamOrder NoParamOrder

see Convention: Named Parameters

fallback double

optional fallback if conversion fails

required bool?

throw error if the name doesn't exist, see Convention: Property Required Name Checks

Returns

double

Value as double

Float(string, NoParamOrder, float, bool?)

Get a property and return the value as a float. If conversion fails, will return default 0 or what is specified in the fallback.

float Float(string name, NoParamOrder noParamOrder = default, float fallback = 0, bool? required = null)

Parameters

name string

property name

noParamOrder NoParamOrder

see Convention: Named Parameters

fallback float

optional fallback if conversion fails

required bool?

throw error if the name doesn't exist, see Convention: Property Required Name Checks

Returns

float

Value as float

Get(string, NoParamOrder, bool?)

Get a property.

object Get(string name, NoParamOrder noParamOrder = default, bool? required = null)

Parameters

name string

the property name like Image - or path to sub-property like Author.Name (new v15)

noParamOrder NoParamOrder

see Convention: Named Parameters

required bool?

throw error if name doesn't exist, see Convention: Property Required Name Checks

Returns

object

The result if found or null; or error if the object is in strict mode

Get<TValue>(string, NoParamOrder, TValue, bool?)

Get a value using the name - and cast it to the expected strong type. For example to get an int even though it's stored as decimal.

Since the parameter fallback determines the type TValue you can just write this like `something.Get("Title", fallback: "no title")

TValue Get<TValue>(string name, NoParamOrder noParamOrder = default, TValue fallback = default, bool? required = null)

Parameters

name string

the property name like Image - or path to sub-property like Author.Name (new v15)

noParamOrder NoParamOrder

see Convention: Named Parameters

fallback TValue

the fallback value to provide if not found

required bool?

throw error if name doesn't exist, see Convention: Property Required Name Checks

Returns

TValue

The typed value, or the default like null or 0 if casting isn't possible.

Type Parameters

TValue

The expected type, like string, int, etc. Note that you don't need to specify it, if you specify the fallback property.

Remarks

Added in v15

Int(string, NoParamOrder, int, bool?)

Get a property and return the value as a int. If conversion fails, will return default 0 or what is specified in the fallback.

int Int(string name, NoParamOrder noParamOrder = default, int fallback = 0, bool? required = null)

Parameters

name string

property name

noParamOrder NoParamOrder

see Convention: Named Parameters

fallback int

optional fallback if conversion fails

required bool?

throw error if the name doesn't exist, see Convention: Property Required Name Checks

Returns

int

Value as int

IsEmpty(string, NoParamOrder)

Check if this typed object has a property of this specified name, and has real data. The opposite version of this is IsNotEmpty(...)

Important

This method is optimized for use in Razor-like scenarios. It's behavior is super-useful but maybe not always expected.

  • If the value is a string, and is empty or only contains whitespace (even &nbsp;) it is regarded as empty.
  • If the returned value is an empty list (eg. a field containing relationships, without any items in it) it is regarded as empty.

If you need a different kind of check, just .Get(...) the value and perform the checks in your code.

bool IsEmpty(string name, NoParamOrder noParamOrder = default)

Parameters

name string

the property name like Image; some objects also support path to sub-property like Author.Name

noParamOrder NoParamOrder

see Convention: Named Parameters

Returns

bool

true if the property exists and has a real value. If it would return an empty list, it will also return false

Remarks

Adding in 16.03 (WIP)

IsNotEmpty(string, NoParamOrder)

Check if this typed object has a property of this specified name, and has real data. The opposite version of this is IsEmpty(...)

Important

This method is optimized for use in Razor-like scenarios. It's behavior is super-useful but maybe not always expected.

  • If the value is a string, and is empty or only contains whitespace (even &nbsp;) it is regarded as empty.
  • If the returned value is an empty list (eg. a field containing relationships, without any items in it) it is regarded as empty.

If you need a different kind of check, just .Get(...) the value and perform the checks in your code.

bool IsNotEmpty(string name, NoParamOrder noParamOrder = default)

Parameters

name string

the property name like Image; some objects also support path to sub-property like Author.Name

noParamOrder NoParamOrder

see Convention: Named Parameters

Returns

bool

true if the property exists and has a real value. If it would return an empty list, it will also return false

Remarks

Adding in 16.03 (WIP)

Keys(NoParamOrder, IEnumerable<string>)

Get all the keys available in this Model (all the parameters passed in). This is used to sometimes run early checks if all the expected parameters have been provided.

IEnumerable<string> Keys(NoParamOrder noParamOrder = default, IEnumerable<string> only = null)

Parameters

noParamOrder NoParamOrder

see Convention: Named Parameters

only IEnumerable<string>

Only return the keys specified here, if found. Typical use: only: new [] { "Key1", "Key2" }. Useful to check if all or any specific keys exist.

Returns

IEnumerable<string>

Remarks

Added in 16.03

Long(string, NoParamOrder, long, bool?)

Get a property and return the value as a long. If conversion fails, will return default 0 or what is specified in the fallback.

long Long(string name, NoParamOrder noParamOrder = default, long fallback = 0, bool? required = null)

Parameters

name string

property name

noParamOrder NoParamOrder

see Convention: Named Parameters

fallback long

optional fallback if conversion fails

required bool?

throw error if the name doesn't exist, see Convention: Property Required Name Checks

Returns

long

Value as long

String(string, NoParamOrder, string, bool?, object)

Get a property and return the value as a string. If conversion fails, will return default null or what is specified in the fallback.

string String(string name, NoParamOrder noParamOrder = default, string fallback = null, bool? required = null, object scrubHtml = null)

Parameters

name string

property name

noParamOrder NoParamOrder

see Convention: Named Parameters

fallback string

optional fallback if conversion fails

required bool?

throw error if the name doesn't exist, see Convention: Property Required Name Checks

scrubHtml object

If true, will remove all HTML tags from the string. If p will remove all p tags, if div,span will remove these tags. This is the same as using Kit.Scrub.All(...) or .Only(...). For more detailed scrubbing, use the Kit.Scrub`

Returns

string

Value as string

Url(string, NoParamOrder, string, bool?)

Get a url from a field. It will do sanitation / url-corrections for special characters etc.

On TypedItems it will also auto-convert values such as file:72 or page:14.

string Url(string name, NoParamOrder noParamOrder = default, string fallback = null, bool? required = null)

Parameters

name string

The field name.

noParamOrder NoParamOrder

see Convention: Named Parameters

fallback string

optional fallback if conversion fails

required bool?

throw error if the name doesn't exist, see Convention: Property Required Name Checks

Returns

string

A url converted if possible. If the field contains anything else such as hello then it will not be modified.