在 C# 中使用 Castle.DynamicProxy 对数组进行排序

Sort an array using Castle.DynamicProxy in C#

我正在努力学习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)
    {
        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
    }
}