実装したいこと
AOPライブラリ Fodyを利用して一回目の処理結果を利用して二回目の処理を行わせるデコレーター twiceを実装したいです。
呼び出し元
C#
1 [twice] 2 public double twice_test(double arg, bool exit = false) 3 { 4 return arg * arg; 5 }
デコレーター
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Reflection; 7 8 9[module: FodySample_twice.twice] 10namespace FodySample_twice 11{ 12 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Assembly | AttributeTargets.Module)] 13 public class twice : Attribute 14 { 15 private object instance; 16 private MethodBase method; 17 private object[] args; 18 19 20 public virtual void Init(object instance, MethodBase method, object[] args) 21 { 22 this.instance = instance; 23 this.method = method; 24 this.args = args; 25 26 //argに代入する 27 if ((bool)args[1] != true) 28 { 29 args[1] = true; 30 args[0] = method.Invoke(instance, args); 31 } 32 33 } 34 35 public void OnEntry() 36 { 37 Console.WriteLine(); 38 Console.WriteLine("Entry Twice"); 39 Console.WriteLine("Param Check Entry"); 40 foreach (object temp in args) 41 { 42 Console.WriteLine(temp); 43 } 44 } 45 46 public void OnExit() 47 { 48 //Console.WriteLine("Param Check Exit"); 49 //foreach (object temp in args) 50 //{ 51 // Console.WriteLine(temp); 52 //} 53 } 54 55 public void OnException(Exception exception) 56 { 57 } 58 } 59} 60
問題点と判明していること
実際に処理される側に戻り値が反映されていません。
デコレーター側では引数として正しく書き換えが行われていることが判明しました。
あなたの回答
tips
プレビュー