2009-06-04 72 views
2

背景德爾福棱鏡Cirrus的訪問和設置功能

這個問題涉及到新Cirrus基礎設施在德爾福棱鏡面向方面編程的結果。

我目前有一個方面,我自動注入一個類,並試圖修改目標代碼使用aMethod.SetBody函數。我已經使用Cirrus Introduction文檔維基上的Logging示例代碼作爲基礎,結構化了我的代碼。

問題

我如何可以訪問函數的結果被注入,既沒有原來的函數體被執行?

我希望能夠在一個代碼路徑中設置繞過對OriginalBody調用的函數的結果,並將其作爲調用OriginalBody的其他代碼路徑,並在我的Aspect代碼中使用OriginalBody的後續Result。 我原本以爲這可能是Aspects.RequireResult方法的預期目的,但這似乎強制執行我的情況下的OriginalBody,導致代碼重複。

回答

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; 
+0

如果我嘗試使用中的字符串Datavalue(目標函數返回一個字符串)我得到一個內部錯誤。如果我使用字符串,這個代碼是否需要更新? – jamiei 2009-06-10 17:10:15