Search Results for

    Show / Hide Table of Contents

    Реализация public свойств и методов класса

    Метод AddClass автоматически добавляет все published свойства класса. Public свойства и методы требуют дополнительной работы. Следующий пример демонстрирует как добавить рublic-метод в класс. Вам потребуется создать дескриптор метода (функция типа TfsCallMethod).

    begin 
      ... 
      { добавим новый класс, унаследованный от TObject } 
      with fsScript1.AddClass(TList, 'TObject') do 
      begin 
        { добавим public методы } 
        AddMethod('function Add(Item: TObject): Integer', CallMethod); 
        AddMethod('procedure Clear', CallMethod); 
      end; 
      ... 
    end; 
    
    { обработчик }
    function TForm1.CallMethod(Instance: TObject; ClassType: TClass; 
      const MethodName: String; var Params: Variant): Variant; 
    begin 
      Result := 0; 
    
      if MethodName = 'ADD' then 
      { преобразуем параметр типа Variant к типу Pointer и вызовем с ним метод Add }  
        TList(Instance).Add(Pointer(Integer(Params[0]))) 
      else if MethodName = 'CLEAR' then 
        TList(Instance).Clear 
    end; 
    

    Для реализации свойства вам потребуется создать дескриптор метода и два дескриптора свойства типов TfsGetValueEvent и TfsSetValueEvent:

      TfsGetValueEvent = function(Instance: TObject; ClassType: TClass; const PropName: String): Variant of object;
      TfsSetValueEvent = procedure(Instance: TObject; ClassType: TClass; const PropName: String; Value: Variant) of object;
    

    Индексируемые (indexed) свойства и свойства по умолчанию (default) описываются дескриптором метода, обычные свойства описываются дескрипторами свойств Get/Set.

    begin 
      ... 
      with fsScript1.AddClass(TStrings, 'TPersistent') do 
      begin 
        { property CommaText: String } 
        AddProperty('CommaText', 'string', GetProp, SetProp); 
        { property Count: Integer readonly, second handler is nil } 
        AddProperty('Count', 'Integer', GetProp, nil); 
        { index property Objects[Index: Integer]: TObject } 
        AddIndexProperty('Objects', 'Integer', 'TObject', CallMethod); 
        { default property Strings[Index: Integer]: String } 
        AddDefaultProperty('Strings', 'Integer', 'string', CallMethod); 
      end; 
      ... 
    end; 
    
    { обработчик }
    function TForm1.CallMethod(Instance: TObject; ClassType: TClass; 
      const MethodName: String; var Params: Variant): Variant; 
    begin 
      Result := 0; 
    
      if MethodName = 'OBJECTS.GET' then 
        Result := Integer(TStrings(Instance).Objects[Params[0]]) 
      else if MethodName = 'OBJECTS.SET' then 
        TStrings(Instance).Objects[Params[0]] := TObject(Integer(Params[1])) 
      else if MethodName = 'STRINGS.GET' then 
        Result := TStrings(Instance).Strings[Params[0]] 
      else if MethodName = 'STRINGS.SET' then 
        TStrings(Instance).Strings[Params[0]] := Params[1] 
    end; 
    
    { обработчик }
    function TForm1.GetProp(Instance: TObject; ClassType: TClass; 
      const PropName: String): Variant; 
    begin 
      Result := 0; 
    
      if PropName = 'COMMATEXT' then 
        Result := TStrings(Instance).CommaText 
      else if PropName = 'COUNT' then 
        Result := TStrings(Instance).Count 
    end; 
    
    { обработчик }
    procedure TForm1.SetProp(Instance: TObject; ClassType: TClass; 
      const PropName: String; Value: Variant); 
    begin 
      if PropName = 'COMMATEXT' then 
        TStrings(Instance).CommaText := Value 
    end; 
    
    Back to top © ООО «Быстрые отчеты»