CodeGen编写自定义表达式标记
CodeGen編寫自定義表達式標記
CodeGen支持開發人員通過編寫plug-in modules插件模塊來定義自定義表達式標記的能力,以提供與這些標記相關聯的邏輯。這種plug-in modules插件機制的實現方式不需要開發人員編輯核心CodeGen源文件。這一點很重要,因為這意味著它不會妨礙將來將源代碼更新下載到核心CodeGen環境的能力。
編寫自定義表達式標記
類在程序集表達式中實現為自定義表達式。為了實現自定義表達式標記,開發人員創建一個包含一個或多個擴展類的類庫程序集,并將該庫與其他CodeGen程序集一起放入主CodeGen文件夾。
如果希望從其他位置加載自定義擴展,則可以將環境變量CODEGEN_EXTDIR設置為自定義令牌擴展程序集的位置。
當CodeGen加載時,它將檢查是否有任何自定義標記程序集,如果找到任何自定義標記程序集,它將動態加載它們。為了實現這一點,使用了命名約定。任何自定義擴展程序集的名稱必須以單詞“custom”開頭。例如,您可以選擇創建一個名為自定義CustomTokens.dll。
實現自定義表達式標記的每個類,都必須實現接口CodeGen.Engine.IExpressionToken可以通過添加對代碼CodeGenEngine.dll匯編庫。
源代碼示例
CodeGen源代碼包包括一個名為CustomExtensionsExample的示例項目,其中包含實現所有類型的自定義表達式標記的示例。此項目被配置為在生成主解決方案時不生成,只是一個示例。鼓勵開發人員在單獨的解決方案中開發自定義表達式處理器。
下面的代碼顯示了自定義字段循環表達式標記的示例:
import System
import System.Collections.Generic
import CodeGen.Engine
import CodeGen.RepositoryAPI
namespace CustomExtensionsExample
;;To implement a custom expression you must build a class that implements the
;;CodeGen.Engine.IExpressionToken interface. The class MUST have a default constructor.
;;By default classes have an implicit default constructor, but if you need to
;;explicitly define a constructor, make sure you don’t define any parameters.
;;
;;You can use this expression in field loops, like this:
;;
;; <FIELD_LOOP>
;; If you see YES then the expression evaluated to true: YES
;; </FIELD_LOOP>
;;
public class CustomFieldLoopExpression implements IExpressionToken
public property TokenName, String
method get
proc
mreturn “CUSTOM_FIELD_LOOP_EXPRESSION”
endmethod
endproperty
public property Description, String
method get
proc
mreturn “An example of a custom field loop expression.”
endmethod
endproperty
public property Validity, TokenValidity
method get
proc
mreturn TokenValidity.FieldLoop
endmethod
endproperty
public method Evaluate, Boolean
tkn, @Token
template, @FileNode
loops, @IEnumerable
endparams
proc
lambda doEvaluate(str, field, index)
begin
;TODO: Add code here to determine the result of the expression, and return true or false
mreturn true
end
mreturn ExpressionEvaluator.EvaluateFieldLoopExpression(tkn, template, loops, doEvaluate)
endmethod
endclass
endnamespace
總結
以上是生活随笔為你收集整理的CodeGen编写自定义表达式标记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeGen CreateFile实用
- 下一篇: CodeGen用户定义的扩展令牌