2

背景

这个问题与Delphi Prism 中面向方面编程的新Cirrus基础设施有关。

我目前有一个方面,我正在自动注入到一个类中,并试图使用aMethod.SetBody函数修改目标代码。到目前为止,我使用Cirrus 介绍文档 wiki 上的 Logging 示例代码作为基础构建了我的代码。

问题

在执行和不执行原始函数体的情况下,如何访问注入的函数的结果?

我希望能够将函数的结果设置为在一个代码路径中绕过对 OriginalBody 的调用,并将其设置为另一个代码路径来调用 OriginalBody 并在我的 Aspect 代码中使用 OriginalBody 的后续结果。我最初认为这可能是Aspects.RequireResult方法的预期目的,但这似乎在我的情况下强制执行 OriginalBody,导致代码重复。

4

1 回答 1

2

你的意思是这样的吗?

原始方法:-

method Something.SomeMethod(a:Integer;b:Integer;c:Integer): Integer;
begin
    result:=b+c;
end;

新方法:-

begin
 if (a > 0) then 
 begin
   result := (b + c);
   exit
   end;
 begin
 result := 1000;
 exit
end

方法级别方面看起来像这样

  [AttributeUsage(AttributeTargets.Method)]
  Class1Attribute = public class(System.Attribute,
    IMethodImplementationDecorator)
  private
  protected
  public
    method HandleImplementation(Services: RemObjects.Oxygene.Cirrus.IServices; aMethod: RemObjects.Oxygene.Cirrus.IMethodDefinition);
  end;

implementation

method Class1Attribute.HandleImplementation(Services: RemObjects.Oxygene.Cirrus.IServices; aMethod: RemObjects.Oxygene.Cirrus.IMethodDefinition);
begin

  var newVersion:=new ResultValue();

  var newAssignment:=new AssignmentStatement(newVersion,new DataValue(1001));

  var p1:= new ParamValue(0);

  aMethod.SetBody(Services,method
    begin
      if (unquote<Integer>(p1)>0) then
      begin
        Aspects.OriginalBody;
      end
      else
      begin
        unquote(newAssignment);
      end;
    end);

end;
于 2009-06-05T04:49:23.553 回答