Interface ITyped
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.
Namespace: ToSic.Sxc.Data
Assembly: ToSic.Sxc.dll
Syntax
[PublicApi]
[JsonConverter(typeof(DynamicJsonConverter))]
public interface ITyped
Remarks
New in 16.02.
Methods
| Improve this Doc View SourceAttribute(string, string, 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:
- Ensure dates are in the ISO format
- Ensure numbers are in a neutral format such as
14.27
and never14,27
- Html encode any characters which would cause trouble such as quotes
Declaration
IRawHtmlString Attribute(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", string fallback = null, bool? required = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | Name of the property |
string | noParamOrder | |
string | fallback | Value to use if the property specified by |
bool? | required | throw error if |
Returns
Type | Description |
---|---|
IRawHtmlString |
Bool(string, string, 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
.
Declaration
bool Bool(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", bool fallback = false, bool? required = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | property name |
string | noParamOrder | |
bool | fallback | optional fallback if conversion fails |
bool? | required | throw error if the |
Returns
Type | Description |
---|---|
bool | Value as |
ContainsKey(string)
Check if this typed object has a property of this specified name. It's case insensitive.
Declaration
bool ContainsKey(string name)
Parameters
Type | Name | Description |
---|---|---|
string | name | the name like |
Returns
Type | Description |
---|---|
bool |
Remarks
Adding in 16.03 (WIP)
DateTime(string, string, 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
.
Declaration
DateTime DateTime(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", DateTime fallback = default, bool? required = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | property name |
string | noParamOrder | |
DateTime | fallback | optional fallback if conversion fails |
bool? | required | throw error if the |
Returns
Type | Description |
---|---|
DateTime | Value as |
Decimal(string, string, 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
.
Declaration
decimal Decimal(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", decimal fallback = 0, bool? required = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | property name |
string | noParamOrder | |
decimal | fallback | optional fallback if conversion fails |
bool? | required | throw error if the |
Returns
Type | Description |
---|---|
decimal | Value as |
Double(string, string, 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
.
Declaration
double Double(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", double fallback = 0, bool? required = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | property name |
string | noParamOrder | |
double | fallback | optional fallback if conversion fails |
bool? | required | throw error if the |
Returns
Type | Description |
---|---|
double | Value as |
Float(string, string, 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
.
Declaration
float Float(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", float fallback = 0, bool? required = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | property name |
string | noParamOrder | |
float | fallback | optional fallback if conversion fails |
bool? | required | throw error if the |
Returns
Type | Description |
---|---|
float | Value as |
Get(string, string, bool?)
Get a property.
Declaration
object Get(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", bool? required = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | the property name like |
string | noParamOrder | |
bool? | required | throw error if |
Returns
Type | Description |
---|---|
object | The result if found or null; or error if the object is in strict mode |
Get<TValue>(string, string, 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")
Declaration
TValue Get<TValue>(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", TValue fallback = default, bool? required = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | the property name like |
string | noParamOrder | |
TValue | fallback | the fallback value to provide if not found |
bool? | required | throw error if |
Returns
Type | Description |
---|---|
TValue | The typed value, or the |
Type Parameters
Name | Description |
---|---|
TValue | The expected type, like |
Remarks
Added in v15
Int(string, string, 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
.
Declaration
int Int(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", int fallback = 0, bool? required = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | property name |
string | noParamOrder | |
int | fallback | optional fallback if conversion fails |
bool? | required | throw error if the |
Returns
Type | Description |
---|---|
int | Value as |
IsEmpty(string, string)
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
) 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.
Declaration
bool IsEmpty(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)")
Parameters
Type | Name | Description |
---|---|---|
string | name | the property name like |
string | noParamOrder |
Returns
Type | Description |
---|---|
bool |
|
Remarks
Adding in 16.03 (WIP)
IsNotEmpty(string, string)
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
) 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.
Declaration
bool IsNotEmpty(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)")
Parameters
Type | Name | Description |
---|---|---|
string | name | the property name like |
string | noParamOrder |
Returns
Type | Description |
---|---|
bool |
|
Remarks
Adding in 16.03 (WIP)
Keys(string, 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.
Declaration
IEnumerable<string> Keys(string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", IEnumerable<string> only = null)
Parameters
Type | Name | Description |
---|---|---|
string | noParamOrder | |
IEnumerable<string> | only | Only return the keys specified here, if found.
Typical use: |
Returns
Type | Description |
---|---|
IEnumerable<string> |
Remarks
Added in 16.03
Long(string, string, 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
.
Declaration
long Long(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", long fallback = 0, bool? required = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | property name |
string | noParamOrder | |
long | fallback | optional fallback if conversion fails |
bool? | required | throw error if the |
Returns
Type | Description |
---|---|
long | Value as |
String(string, string, 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
.
Declaration
string String(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", string fallback = null, bool? required = null, object scrubHtml = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | property name |
string | noParamOrder | |
string | fallback | optional fallback if conversion fails |
bool? | required | throw error if the |
object | scrubHtml | If |
Returns
Type | Description |
---|---|
string | Value as |
Url(string, string, 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
.
Declaration
string Url(string name, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", string fallback = null, bool? required = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | The field name. |
string | noParamOrder | |
string | fallback | optional fallback if conversion fails |
bool? | required | throw error if the |
Returns
Type | Description |
---|---|
string | A url converted if possible. If the field contains anything else such as |