I am creating a C# TBB(C# Code fragment). For that I need to write a userdefined method. I tried creating it using <%! %>. How to access the user defined method in the code. Thanks in advance. Please suggest me a way to solve this issue.
问题:
回答1:
The TOM.NET API reference provides the following example:
<%@ Import Namespace="Tridion.ContentManager.Publishing"%>
<%!
private string ExtraString()
{
return "Something added by the C# template";
}
%>
log.Debug("Executing C# template");
if (engine.RenderMode == RenderMode.Publish)
{
package.GetByName(Package.OutputName).AppendToStringValue(ExtraString());
}
In addition to the above, the following syntax is supported:
<%@Import Namespace="..." %>
Imports the namespace enclosed between the quotation marks into the code fragment. Any class you import must be present in the Global Assembly Cache.
<%! ... %>
Declares methods, constants and classes for the rest of the code fragment to use. The declarations cannot contain the string '%>'. Note that any classes you create can only be referenced within the code fragment.
<%RunTemplate Template="tcm:1-184-2048" Class="Tridion.Templating.Examples.ExampleTemplateClass"%>
Runs a specific .NET Assembly Template Building Block, identified by the URI in the Template attribute. This statement is typically generated by SDL Tridion 2009 itself when you upload a .NET assembly, to provide access to a specific class in the .NET Assembly.
<%@Assembly Name="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"%>
Inserts a reference to a nonstandard .NET assembly, which must be present in the Global Assembly Cache. Use the full assembly name.
回答2:
Here is the documentation reference link with example that Nickol pointing.
回答3:
Check these below links for complete details on creating Functions as well as Classes in a C# Code Fragment.
Functions: https://rcnitesh.wordpress.com/2015/04/23/functions-in-c-tbb/
[For full details including code samples, check the above link ]Classes: https://rcnitesh.wordpress.com/2015/04/24/creating-user-defined-classes-in-c-code-fragment/ Below is an excerpt from the above Blog:
Classes are defined inside a C# Code Fragment using the construct: <%! %>
.
This USER defined class is placed as NESTED CLASS of the PREDEFINED CLASS created by Tridion when compiling the C# Code fragment. Check this blog post for in depth details on C# Code Fragment compilation and the Predefined classes , methods generated by tridion together with understanding the relation of Tridion’s Predefined class with User defined Class in a C# Code Fragment
That said above, what we need to understand is that the User defined classes in a C# Code Fragment cannot access the predefined variables like: log, engine, and package , since these variables are actually declared Private in the Predefined class generated during compilation.
[For full details including code samples, check the above links ]