0

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.

ImC0der
  • 308
  • 2
  • 18

1 Answers1

0

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
    }
}
timur
  • 14,239
  • 2
  • 11
  • 32
  • I got error. 'Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'source') at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument) at System.Linq.Enumerable.Reverse[TSource](IEnumerable`1 source) at YonOdaklıProje.Interceptor.Intercept(IInvocation invocation) in line 16 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.KoleksiyonDöndürProxy.ReturnListStringData() at YonOdaklıProje.Program.Main(String[] args) in line 13' – ImC0der Dec 05 '19 at 07:58
  • You mind setting a breakpoint, inspecting `invocation` in debugger and checking what `ReturnValue` is? I suspect it's either null or not array of string for whatever reason. So whatever type it is - you'd have to adjust the type cast. – timur Dec 05 '19 at 08:22
  • Oh I solved it thanks. I want to print it to screen with console writline. System.String[] says on the screen. How can i see array's elements? – ImC0der Dec 05 '19 at 08:28
  • The same way you do it in `ArrayStringData` is an option: after your reverse line, do something like `foreach (var item in invocation.ReturnValue) { Console.WriteLine(item); }` – timur Dec 05 '19 at 08:30
  • I tried it and "foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public instance definition for 'GetEnumerator'" – ImC0der Dec 05 '19 at 08:33
  • Oh, true is an object now. I'll update the answer in a sec – timur Dec 05 '19 at 08:34