Question

I'm using unity interception to audit a method call. Everything works fine: the method is intercepted and I can get the method name and other thinks. I also want to get the result of my method. For example, if my method returns a List object I'm only able to access the IMethodReturn.ReturnValue, whose type is an object. In my case, the underlying type of the result is List so I can cast the IMethodReturn.ReturnValue return object like this. (List)IMethodReturn.ReturnValue Problem: I have to make this work when I don't know the return type of the intercepted method. That is, the execution of the intercepted method sometimes can return a string type, other a List, other a List and so on. The posibilities are huge. I need, if possible, a way to convert the IMethodReturn.ReturnValue to the underlying type of the return value of the intercepted method. The final target is to transform the method result to xml and to save the data in a database. For audit purposes.

Here is the code (sorry for format...)

/// Invoke method public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { IMethodReturn resultData = getNext()(input, getNext); // Injection after method execution. this.InjectionCall(input, resultData);
return resultData; }

// Here is the problem... private void InjectionCall(IMethodInvocation input, IMethodReturn result) { string methodName = input.MethodBase.Name;

// How to get the 
//List<object> resultList = (List<object>)result.ReturnValue   ???
//Type type = resultData.ReturnValue.GetType()

// Calling the audit service. IocFactory is the container helper object.
//IAuditService srvAudit = IocFactory.Resolve<IAuditService>();
//srvAudit.RegisterData(methodName,null,null);

}

Thanks.

Was it helpful?

Solution

The real problem was the transformation from the object returned by the intercepted method more than a problem of Interception itself. I solved it by serializing the object to Xml. The solution I have found is in this link:

Serialize object to XmlDocument

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top