Unity The Method Signature Matching Rule
Unity提供了用于匹配函数名称和类型签名的规则MethodSignatureMatchingRule类型。ParameterTypeMatchingRule用于匹配函数的参数类型和参数性质(In、Out),但是无法匹配函数名称。而MethodSignatureMatchingRule的缺点是无法限制参数性质。看一个简单示例:
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 String[] paramTypeNames = new String[] { “System.Int32″, “System.Char” }; 24 25 unityContainer.Configure<Interception>() 26 .AddPolicy(“MethodSignatureMatchingRule”) 27 .AddMatchingRule(new MethodSignatureMatchingRule(“DoWork*”, paramTypeNames)) 28 .AddCallHandler<Log4NetHandler>(); 29 unityContainer.RegisterType<MyObject>( 30 new Interceptor<VirtualMethodInterceptor>(), 31 new InterceptionBehavior<PolicyInjectionBehavior>() 32 ); 33 34 MyObject myObject = unityContainer.Resolve<MyObject>(); 35 36 myObject.DoWork(Int32.MaxValue, Char.MaxValue); 37 myObject.DoWork2(Int32.MaxValue, Char.MaxValue); 38 myObject.DoWork3();
相比ParameterTypeMatchingRule,MethodSignatureMatchingRule结合了ParameterTypeMatchingRule和MemberNameMatchingRule的功能,只是它无法限制参数的性质是In、Out。配置文件定义如下:
<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” /> <namespace name=”System.Collections.Generic” /> <namespace name=”Microsoft.Practices.Unity.InterceptionExtension” /> <namespace name=”UnityTest6″ /> <container> <extension type=”Interception” /> <register name=”paramTypeNames” type=”IEnumerable[String]” mapTo=”List[String]“> <constructor /> <method name=”Add”> <param name=”item” value=”System.Int32″ /> </method> <method name=”Add”> <param name=”item” value=”System.Char” /> </method> </register> <interception> <policy name=”MethodSignaturePolicy”> <matchingRule name=”MethodSignatureMatchingRule” type=”MethodSignatureMatchingRule”> <constructor> <param name=”methodName” value=”DoWork*” /> <param name=”parameterTypeNames” dependencyName=”paramTypeNames” /> </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速度为什么快?