我正在尝试学习AOP。我有一个返回数组的方法。
public class ReturnCollection
{
public virtual Array ReturnArrayStringData()
{
string[] IntArray = { "1", "a", "4", "'", "&", "g" };
foreach (var item in IntArray)
{
Console.WriteLine(item);
}
return IntArray;
}
}
我想使用Castle.DynamicProxy和ArrayName.sort()对数组进行排序。这是我的代理方法。
[Serializable]
public abstract class Interceptor: IInterceptor
{
protected abstract void ExecuteAfter(IInvocation invocation);
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
ExecuteAfter(invocation);
}
}
public class ExecuteAfterMethod: Interceptor
{
protected override void ExecuteAfter(Castle.DynamicProxy.IInvocation invocation)
{
//sort array here.
}
}
我该怎么办?谢谢。
假设您只需要实现该方法,一种选择是重新分配invocation.ReturnValue
:
public class ExecuteAfterMethod : Interceptor
{
protected override void ExecuteAfter(Castle.DynamicProxy.IInvocation invocation)
{
invocation.ReturnValue = (invocation.ReturnValue as string[]).Reverse().ToArray();
}
}