Class DynamicCode
This is a base class for dynamic code which is compiled at runtime.
Tip
This is an old base class and works, but you should use a newer one such as CodeTyped
Namespace: ToSic.Sxc.Code
Assembly: ToSic.Sxc.dll
Syntax
[PublicApi]
public abstract class DynamicCode : DynamicCodeBase, ICanDebug, IHasCodeLog, IDynamicCode, IHasLog
Constructors
| Improve this Doc View SourceDynamicCode()
Main constructor, to enable easy inheriting in custom code.
Declaration
protected DynamicCode()
Properties
| Improve this Doc View SourceApp
A fully prepared IApp object letting you access all the data and queries in the current app.
Declaration
public IApp App { get; }
Property Value
Type | Description |
---|---|
IApp | The current app |
CmsContext
This Context tells you about the environment, such as
- the current User
- the Page
- the View
- the Site
It's supposed to replace direct access to Dnn or Oqtane object in Razor and WebAPI code, allowing hybrid code that works everywhere.
Declaration
public ICmsContext CmsContext { get; }
Property Value
Type | Description |
---|---|
ICmsContext |
Remarks
New in v11.11
Content
The content object of the current razor view - IF the current view has content. If the view is a list, it will return the first item. Will be null otherwise. To tell if it's the demo/default item, use IsDemoItem.
Declaration
public dynamic Content { get; }
Property Value
Type | Description |
---|---|
dynamic | A IDynamicEntity object with the current content - or null. |
Data
The data prepared for the current Code. Usually user data which was manually added to the instance, but can also be a query.
Declaration
public IContextData Data { get; }
Property Value
Type | Description |
---|---|
IContextData | An IContextData which is as IDataSource. |
Edit
Helper commands to enable in-page editing functionality Use it to check if edit is enabled, generate context-json infos and provide toolbar buttons
Declaration
public IEditService Edit { get; }
Property Value
Type | Description |
---|---|
IEditService | An IEditService object. |
Header
The header object of the current razor view, if it's a list and has a header object. If it's a list and doesn't have a header (and no default), it will return null. To tell if it's the demo/default item, use IsDemoItem.
Declaration
public dynamic Header { get; }
Property Value
Type | Description |
---|---|
dynamic | A IDynamicEntity object with the current content. |
Remarks
Introduced in 2sxc 10.10 - previously it was called ListContent, now deprecated.
Link
Link helper object to create the correct links
Declaration
public ILinkService Link { get; }
Property Value
Type | Description |
---|---|
ILinkService | A ILinkService object. |
Log
The logger for the current Razor / WebApi which allows you to add logs to Insights.
Declaration
public ICodeLog Log { get; }
Property Value
Type | Description |
---|---|
ICodeLog |
Methods
| Improve this Doc View SourceAsAdam(ICanBeEntity, string)
Provides an Adam instance for this item and field
Declaration
public IFolder AsAdam(ICanBeEntity item, string fieldName)
Parameters
Type | Name | Description |
---|---|---|
ICanBeEntity | item | The item - an IEntity, IDynamicEntity, ITypedItem etc. often Content or similar |
string | fieldName | The field name, like "Gallery" or "Pics" |
Returns
Type | Description |
---|---|
IFolder | An Adam object for navigating the assets |
AsDynamic(object)
Convert a dynamic entity and return itself again. This is so coders don't have to worry if the original object was an IEntity or a IDynamicEntity in the first place.
Declaration
public dynamic AsDynamic(object dynamicEntity)
Parameters
Type | Name | Description |
---|---|---|
object | dynamicEntity | the original object |
Returns
Type | Description |
---|---|
dynamic | a dynamic object for easier coding |
AsDynamic(params object[])
Convert one or many Entities and Dynamic entities into an IDynamicStack
Declaration
public dynamic AsDynamic(params object[] entities)
Parameters
Type | Name | Description |
---|---|---|
object[] | entities | one or more source object |
Returns
Type | Description |
---|---|
dynamic | a dynamic object for easier coding |
Remarks
New in 12.05
AsDynamic(string, string)
Take a json and provide it as a dynamic object to the code
Declaration
public dynamic AsDynamic(string json, string fallback = null)
Parameters
Type | Name | Description |
---|---|---|
string | json | the original json string |
string | fallback | Alternate string to use, if the original json can't parse. Can also be null or the word "error" if you would prefer an error to be thrown. |
Returns
Type | Description |
---|---|
dynamic | A dynamic object representing the original json. If it can't be parsed, it will parse the fallback, which by default is an empty empty dynamic object. If you provide null for the fallback, then you will get null back. |
Remarks
Added in 2sxc 10.22.00
AsDynamic(IEntity)
Wraps an entity into a IDynamicEntity
Declaration
public dynamic AsDynamic(IEntity entity)
Parameters
Type | Name | Description |
---|---|---|
IEntity | entity | the original object |
Returns
Type | Description |
---|---|
dynamic | a dynamic object for easier coding |
AsEntity(object)
Unwraps a dynamic entity or ITypedItem back into the underlying IEntity
Declaration
public IEntity AsEntity(object dynamicEntity)
Parameters
Type | Name | Description |
---|---|---|
object | dynamicEntity | the wrapped IEntity |
Returns
Type | Description |
---|---|
IEntity | A normal IEntity |
AsList(object)
Converts a list of IEntity objects into a list of IDynamicEntity objects.
Declaration
public IEnumerable<dynamic> AsList(object list)
Parameters
Type | Name | Description |
---|---|---|
object | list | typically a List/IEnumerable of Entities or DynamicEntities. |
Returns
Type | Description |
---|---|
IEnumerable<dynamic> | a list of IDynamicEntity objects |
Remarks
Added in 2sxc 10.21.00
CreateInstance(string, string, string, string, bool)
Create an instance of code lying in a file near this
Declaration
public dynamic CreateInstance(string virtualPath, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", string name = null, string relativePath = null, bool throwOnError = true)
Parameters
Type | Name | Description |
---|---|---|
string | virtualPath | path to the other code file to compile |
string | noParamOrder | |
string | name | Override the class name to compile - usually not required as it should match the file name |
string | relativePath | optional relative path, will usually use the CreateInstancePath |
bool | throwOnError | throw errors if compiling fails, recommended |
Returns
Type | Description |
---|---|
dynamic | An object of the class in the file |
Remarks
Note that the C# code which we are creating inherits from a standard base class such as Code12 or DynamicCode then it will automatically be initialized to support App, AsDynamic etc.
CreateSource<T>(IDataSource, ILookUpEngine)
Create a IDataSource which will process data from the given stream.
Declaration
public T CreateSource<T>(IDataSource inSource = null, ILookUpEngine configurationProvider = null) where T : IDataSource
Parameters
Type | Name | Description |
---|---|---|
IDataSource | inSource | The data source which will be the default In of the new data-source. |
ILookUpEngine | configurationProvider | An alternate configuration provider for the DataSource |
Returns
Type | Description |
---|---|
T | A typed DataSource object |
Type Parameters
Name | Description |
---|---|
T | A data-source type - must be inherited from IDataSource |
CreateSource<T>(IDataStream)
Create a IDataSource which will process data from the given stream.
Declaration
public T CreateSource<T>(IDataStream source) where T : IDataSource
Parameters
Type | Name | Description |
---|---|---|
IDataStream | source | The stream which will be the default In of the new data-source. |
Returns
Type | Description |
---|---|
T | A typed DataSource object |
Type Parameters
Name | Description |
---|---|
T | A data-source type - must be inherited from IDataSource |
GetService<TService>()
Get a service from the Dependency Injection. The service can come from 2sxc, EAV or the underlying platform (Dnn, Oqtane).
Declaration
public TService GetService<TService>() where TService : class
Returns
Type | Description |
---|---|
TService | An object of the type or interface requested, or null if not found in the DI. |
Type Parameters
Name | Description |
---|---|
TService | Interface (preferred) or Class which is needed |