Mugen Injection [Interception]
Interception - расширение для MugenInjection предоставляет возможность для перехвата: событий, свойств и методов.
Для начала работы с расширением вам следует:
- Добавить ссылки на библиотеки MugenInjection.Core.dll и MugenInjection.Interception.dll.
- При запуске приложения зарегистрировать компонент с перехватчиком.
- Использовать компонент во время выполнения приложения.
Важное замечание
Ваши методы/свойства, которые будут перехвачены должны быть виртуальным! Если вы посмотрите на все примеры, все члены являются виртуальными.Виды перехватов
MugenInjection предоставляет два вида перехватов:- Перехват осуществляется в новый прокси-класс.
- Перехват осуществляется в новый прокси-класс с зависимым классом (target).
_injector = new MugenInjector(); _injector.Bind<Alpha>().Intercept(new InterceptorProcess()).InSingletonScope(); //Getting proxy of alpha. var alpha = _injector.Get<Alpha>();
_injector = new MugenInjector(); _injector.Bind<Alpha>().ToSelf().InSingletonScope().InterceptAsTarget(new InterceptorProcess()); //Getting proxy of alpha with target of Alpha. var alpha = _injector.Get<Alpha>();
Обработчики для перехвата
MugenInjection предоставляет два вида обработчиков:- ISimpleInterceptorProcess - перехватывает вызов методов, свойств и событий используя один метод.
- IInterceptorProcess - перехватывает вызов методов, свойств и событий.
/// <summary> /// Intercepts the call to action related to methods, properties, and events. /// </summary> public interface ISimpleInterceptorProcess { /// <summary> /// Gets the priority. /// </summary> int Priority { get; } /// <summary> /// Intercepts all actions. /// </summary> /// <param name="interceptor">The specified <see cref="IInterceptor"/>.</param> void Intercept(IInterceptor interceptor); }
/// <summary> /// Intercepts the call to action related to methods, properties, and events. /// </summary> public interface IInterceptorProcess { /// <summary> /// Gets the priority. /// </summary> int Priority { get; } /// <summary> /// Intercepts the method using the specified <see cref="IMethodInterceptor"/>. /// </summary> /// <param name="methodInterceptor">The specified <see cref="IMethodInterceptor"/>.</param> void InterceptMethod(IMethodInterceptor methodInterceptor); /// <summary> /// Intercepts the get property method using the specified <see cref="IPropertyGetInterceptor"/>. /// </summary> /// <param name="propertyGetInterceptor">The specified <see cref="IPropertyGetInterceptor"/></param> void InterceptGetProperty(IPropertyGetInterceptor propertyGetInterceptor); /// <summary> /// Intercepts the set property method using the specified <see cref="IPropertySetInterceptor"/>. /// </summary> /// <param name="propertySetInterceptor">The specified <see cref="IPropertySetInterceptor"/>.</param> void InterceptSetProperty(IPropertySetInterceptor propertySetInterceptor); /// <summary> /// Intercepts the add event method using the specified <see cref="IEventAddInterceptor"/>. /// </summary> /// <param name="eventAddInterceptor">The specified <see cref="IEventAddInterceptor"/>.</param> void InterceptAddEvent(IEventAddInterceptor eventAddInterceptor); /// <summary> /// Intercepts the remove event method using the specified <see cref="IEventRemoveInterceptor"/>. /// </summary> /// <param name="eventRemoveInterceptor">The specified <see cref="IEventRemoveInterceptor"/>.</param> void InterceptRemoveEvent(IEventRemoveInterceptor eventRemoveInterceptor); }
Fluent-syntax для добавления перехватчиков
MugenInjection также предоставляет возможность, зарегистрировать перехватчики, когда вы регистрируете компоненты.Пример кода показывает, как добавить перехватчиков для, 'get-property', 'set-property' и 'method call':
public class Alpha { public virtual string Property { get; set; } public virtual void TestMethod() { } } _injector = new MugenInjector(); _injector.Bind<Alpha>().Intercept() .InterceptGetProperty(alpha => alpha.Property, (target, value, interceptor) => { //target-contains the target, if any. //value-contains the func to get original value, if any. //interceptor-represents the interceptor to intercept property-get. Console.WriteLine("Get property call"); return "test"; }) .InterceptSetProperty(alpha => alpha.Property, (target, value, interceptor) => { //target-contains the target, if any. //value-contains the set value. //interceptor-represents the interceptor to intercept property-set. Console.WriteLine("Set property call"); return "setProperty"; }) .InterceptInvokeMethod((context, alpha) => alpha.TestMethod(), (target, original, interceptor) => { //target-contains the target, if any. //original-contains the delegate to invoke original method. //interceptor-represents the interceptor to intercept method-call. Console.WriteLine("Method call"); }); var a = _injector.Get<Alpha>(); //property equals to "test" var property = a.Property; a.Property = "value"; //property equals to "test" property = a.Property; a.TestMethod();