Einzelnen Beitrag anzeigen

Der schöne Günther

Registriert seit: 6. Mär 2013
6.111 Beiträge
 
Delphi 10 Seattle Enterprise
 
#7

AW: Attribute einer Methode in Methode abfragen

  Alt 13. Aug 2019, 18:26
Ich kann das nicht nachvollziehen. Hast du ein funktionsfähiges Testprogramm?

Ich bekomme hier keine Speicherlecks:

Delphi-Quellcode:
// JCL_DEBUG_EXPERT_GENERATEJDBG OFF
// JCL_DEBUG_EXPERT_INSERTJDBG OFF
program AtrtibTestProject;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  FastMM4,
  System.SysUtils,
  System.Rtti;

type
   MyAttribute = class(TCustomAttribute);

   TMyObject = class(TObject)
      public
         [MyAttribute]
         procedure test(); virtual;
   end;

procedure p();
var
   interceptor: TVirtualMethodInterceptor;
   myObject: TMyObject;
begin
   interceptor := nil; myObject := nil;
   try
      myObject := TMyObject.Create();
      interceptor := TVirtualMethodInterceptor.Create( myObject.ClassType() );
      interceptor.OnBefore :=
         procedure(
            instance:   TObject;
            method:      TRttiMethod;
            const args: TArray<TValue>;
            out doInvoke: Boolean;
            out result: TValue
         )
         var
            attributes: TArray<TCustomAttribute>;
         begin
            attributes := method.GetAttributes();
            WriteLn('We found ', Length(attributes), ' attributes');

         end;
      interceptor.Proxify(myObject);

      myObject.test();
   finally
      if Assigned(interceptor) then
         begin
            interceptor.Unproxify(myObject);
            interceptor.Destroy();
            end;
      myObject.Free();
   end;
end;


{ TMyObject }

procedure TMyObject.test();
begin
   WriteLn('Hello World');
end;

begin
   ReportMemoryLeaksOnShutdown := True;
   try
      p();
   except
      on E: Exception do
         Writeln(E.ClassName, ': ', E.Message);
   end;
   readln;
end.
  Mit Zitat antworten Zitat