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: <IF CUSTOM_FIELD_LOOP_EXPRESSION>YES</IF>
  ;;  </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<LoopNode>
      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

 

posted @ 2020-12-15 07:35  吴建明wujianming  阅读(105)  评论(0编辑  收藏  举报