I am trying to learn AOP. I have a method returning array.
public class ReturnCollection
{
public virtual Array ReturnArrayStringData()
{
string[] IntArray = { "1", "a", "4", "'", "&", "g" };
foreach (var item in IntArray)
{
Console.WriteLine(item);
}
return IntArray;
}
}
I want to sort this array using Castle.DynamicProxy and ArrayName.sort(). Here is my proxy Method.
[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.
}
}
How can I do it? Thanks.
Assuming you just needed to implement the method, one option would be to reassign invocation.ReturnValue
:
public class ExecuteAfterMethod : Interceptor
{
protected override void ExecuteAfter(Castle.DynamicProxy.IInvocation invocation)
{
var stringArray = (invocation.ReturnValue as string[]).Reverse().ToArray(); // here will have the object typed as String[], so printing it should be no problem.
foreach(var item in stringArray) {
Console.WriteLine(item);
}
invocation.ReturnValue = stringArray; // and finally we reassign the result
}
}