Unity The Parameter Type Matching Rule
Unity提供了函数入口参数类型匹配的规则ParameterTypeMatchingRule类型。它可以针对一些特定函数的入口参数进行拦截,比如拦截入口参数类型为Int32和Char的函数,看一个简单示例:
1 public class MyObject 2 { 3 public virtual void DoWork(Int32 i, Char c) 4 { 5 6 } 7 8 public virtual void DoWork2(Int32 i, Char c) 9 { 10 11 } 12 13 public virtual void DoWork3() 14 { 15 16 } 17 } 18 19 IUnityContainer unityContainer = new UnityContainer(); 20 21 unityContainer.LoadConfiguration(); 22 23 ParameterTypeMatchingInfo[] paramsUsed = new ParameterTypeMatchingInfo[] 24 { 25 new ParameterTypeMatchingInfo(“System.Int32″, false, ParameterKind.Input), 26 new ParameterTypeMatchingInfo(“System.Char”, false, ParameterKind.Input) 27 }; 28 29 unityContainer.Configure<Interception>() 30 .AddPolicy(“ParameterTypeMatchingRule”) 31 .AddMatchingRule(new ParameterTypeMatchingRule(paramsUsed)) 32 .AddCallHandler<Log4NetHandler>(); 33 unityContainer.RegisterType<MyObject>( 34 new Interceptor<VirtualMethodInterceptor>(), 35 new InterceptionBehavior<PolicyInjectionBehavior>() 36 ); 37 38 MyObject myObject = unityContainer.Resolve<MyObject>(); 39 40 myObject.DoWork(Int32.MaxValue, Char.MaxValue); 41 myObject.DoWork2(Int32.MaxValue, Char.MaxValue); 42 myObject.DoWork3();
只有MyObject的DoWork和DoWork2函数符合定义的ParameterTypeMatchingRule。虽然ParameterTypeMatchingInfo允许定义返回值和它的类型,但是在ParameterTypeMatchingRule中无效。看一个简单的例子:
1 public class MyObject 2 { 3 public virtual Int32 DoWork(Int32 i, Char c) 4 { 5 return i; 6 } 7 8 public virtual void DoWork2(Int32 i, Char c) 9 { 10 11 } 12 13 public virtual void DoWork3() 14 { 15 16 } 17 } 18 19 IUnityContainer unityContainer = new UnityContainer(); 20 21 unityContainer.LoadConfiguration(); 22 23 ParameterTypeMatchingInfo[] paramsUsed = new ParameterTypeMatchingInfo[] 24 { 25 new ParameterTypeMatchingInfo(“System.Int32″, false, ParameterKind.Input), 26 new ParameterTypeMatchingInfo(“System.Char”, false, ParameterKind.Input), 27 new ParameterTypeMatchingInfo(“System.Int32″, false, ParameterKind.ReturnValue) 28 }; 29 30 unityContainer.Configure<Interception>() 31 .AddPolicy(“ParameterTypeMatchingRule”) 32 .AddMatchingRule(new ParameterTypeMatchingRule(paramsUsed)) 33 .AddCallHandler<Log4NetHandler>(); 34 unityContainer.RegisterType<MyObject>( 35 new Interceptor<VirtualMethodInterceptor>(), 36 new InterceptionBehavior<PolicyInjectionBehavior>() 37 ); 38 39 MyObject myObject = unityContainer.Resolve<MyObject>(); 40 41 myObject.DoWork(Int32.MaxValue, Char.MaxValue); 42 myObject.DoWork2(Int32.MaxValue, Char.MaxValue); 43 myObject.DoWork3();
虽然添加了返回值类型的限制,但是DoWork2依然被拦截注入。一般来说我们可以通过ParameterTypeMatchingRule跟踪那些使用句柄的函数。ParameterTypeMatchingRule的构造比较麻烦,首先它的构造函数接受IEnumerable<ParameterTypeMatchingInfo>类型,需要注册一个List<ParameterTypeMatchingInfo>去实现。然后通过method定义Add函数添加一个一个ParameterTypeMatchingInfo。配置文件定义如下:
<unity xmlns=”http://schemas.microsoft.com/practices/2010/unity”> <sectionExtension type=”Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration” /> <assembly name=”mscorlib, 2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ /> <assembly name=”Microsoft.Practices.Unity.Interception” /> <assembly name=”UnityTest6″ /> <namespace name=”System.Collections.Generic” /> <namespace name=”UnityTest6″ /> <namespace name=”Microsoft.Practices.Unity.InterceptionExtension” /> <container> <extension type=”Interception” /> <register name=”ParameterTypeMatchingInfo1″ type=”ParameterTypeMatchingInfo”> <constructor> <param name=”nameToMatch” value=”System.Int32″ /> <param name=”ignoreCase” value=”false” /> <param name=”kind” value=”Input” /> </constructor> </register> <register name=”ParameterTypeMatchingInfo2″ type=”ParameterTypeMatchingInfo”> <constructor> <param name=”nameToMatch” value=”System.Char” /> <param name=”ignoreCase” value=”false” /> <param name=”kind” value=”Input” /> </constructor> </register> <register name=”paramsUsed” type=”IEnumerable[ParameterTypeMatchingInfo]” mapTo=”List[ParameterTypeMatchingInfo]“> <constructor /> <method name=”Add”> <param name=”item” dependencyName=”ParameterTypeMatchingInfo1″ /> </method> <method name=”Add”> <param name=”item” dependencyName=”ParameterTypeMatchingInfo2″ /> </method> </register> <interception> <policy name=”ParameterTypePolicy”> <matchingRule name=”ParameterTypeMatchingRule” type=”ParameterTypeMatchingRule”> <constructor> <param name=”matches” dependencyName=”paramsUsed” /> </constructor> </matchingRule> <callHandler name=”Log4NetHandler” type=”Log4NetHandler” /> </policy> </interception> <register type=”MyObject”> <interceptor type=”VirtualMethodInterceptor” /> <interceptionBehavior type=”PolicyInjectionBehavior” /> </register> </container> </unity>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?