获取Unity拦截的方法信息(Unity Interception,IMethodReturn)

时间:2010-11-11 17:27:51

标签: unity-container type-conversion interception

我正在使用Unity拦截来审核方法调用。一切正常:方法被截获,我可以得到方法名称和其他想法。我也想得到我的方法的结果。例如,如果我的方法返回一个List对象,我只能访问IMethodReturn.ReturnValue,其类型是一个对象。在我的例子中,结果的基础类型是List,所以我可以像这样强制转换IMethodReturn.ReturnValue返回对象。 (名单)IMethodReturn.ReturnValue 问题: 当我不知道截获方法的返回类型时,我必须完成这项工作。也就是说,截获方法的执行有时可以返回字符串类型,其他List,其他List等。可能性很大。 如果可能的话,我需要一种方法将IMethodReturn.ReturnValue转换为截获方法的返回值的基础类型。 最终目标是将方法结果转换为xml并将数据保存在数据库中。出于审计目的。

这是代码(对不起格式......)

///调用方法 public IMethodReturn Invoke(IMethodInvocation输入,GetNextHandlerDelegate getNext) {     IMethodReturn resultData = getNext()(input,getNext);     //方法执行后注入     this.InjectionCall(input,resultData);
    return resultData; }

//这是问题...... private void InjectionCall(IMethodInvocation输入,IMethodReturn结果) {     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);

}

感谢。

1 个答案:

答案 0 :(得分:1)

真正的问题是拦截方法返回的对象的转换不仅仅是拦截本身的问题。我通过将对象序列化为Xml来解决它。我找到的解决方案是在这个链接中:

Serialize object to XmlDocument