Unity의 세 가지 Interceptor
9749 단어 Interceptor
TransparentProxyInterceptor: 프록시 구현 기반.NET Remoting 기술은 객체의 모든 함수를 차단합니다.단점은 차단된 유형이 MarshalByRefObject에서 파생되어야 한다는 것입니다.예는 다음과 같습니다.
1 public class MyObject : MarshalByRefObject
2 {
3 public String Name { get; set; }
4 }
5
6 IUnityContainer unityContainer = new UnityContainer();
7
8 unityContainer.AddNewExtension<Interception>();
9 unityContainer.RegisterType<MyObject>(new Interceptor<TransparentProxyInterceptor>(), new InterceptionBehavior(new NotifyPropertyChangedBehavior()));
10
11 MyObject myObject = unityContainer.Resolve<MyObject>();
12
13 ((INotifyPropertyChanged)myObject).PropertyChanged += new PropertyChangedEventHandler((sender, e) => Console.WriteLine(e.PropertyName));
14
15 myObject.Name = “hello, world”;
InterfaceInterceptor: 한 인터페이스만 차단할 수 있으며, 목표 유형이 지정한 인터페이스를 실현하면 차단할 수 있습니다.예는 다음과 같습니다.
1 public class MyObject2 : IServiceProvider
2 {
3
4 #region IServiceProvider Members
5
6 public object GetService(Type serviceType)
7 {
8 return null;
9 }
10
11 #endregion
12 }
13
14 public sealed class MyInterceptionBehavior : IInterceptionBehavior
15 {
16 #region IInterceptionBehavior Members
17
18 public Boolean WillExecute
19 {
20 get { return true; }
21 }
22
23 public IEnumerable<Type> GetRequiredInterfaces()
24 {
25 return new Type[0];
26 }
27
28 public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
29 {
30 return getNext()(input, getNext);
31 }
32
33 #endregion
34 }
35
36 IUnityContainer unityContainer = new UnityContainer();
37
38 unityContainer.AddNewExtension<Interception>();
39 unityContainer.RegisterType<IServiceProvider, MyObject2>(“MyObject2″,
40 new Interceptor<InterfaceInterceptor>(),
41 new InterceptionBehavior<MyInterceptionBehavior>()
42 );
43
44 IServiceProvider myObject = unityContainer.Resolve<IServiceProvider>(“MyObject2″);
45
46 myObject.GetService(typeof(MyObject2));
등록할 때 차단된 인터페이스 형식을 표시해야 합니다.
VirtualMethodInterceptor:virtual 함수를 차단합니다.단점은 차단된 유형에 가상 함수가 없으면 차단할 수 없습니다. 이 때 유형이 특정한 인터페이스를 실현하면 인터페이스 인터셉터로 변경할 수 있습니다.간단한 예:
1 public class MyObject3
2 {
3 public virtual void DoWork()
4 {
5
6 }
7 }
8
9 IUnityContainer unityContainer = new UnityContainer();
10
11 unityContainer.AddNewExtension<Interception>();
12 unityContainer.RegisterType<MyObject3>(
13 new Interceptor<VirtualMethodInterceptor>(),
14 new InterceptionBehavior<MyInterceptionBehavior>()
15 );
16
17 MyObject3 myObject = unityContainer.Resolve<MyObject3>();
18
19 myObject.DoWork();
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Nginx] 서버 장애/배포시 점검중 페이지 redirect기존 Nginx 설정파일에는 404 error를 받는 경우 '페이지를 찾을 수 없습니다' 페이지(NotFound.html)로 redirect 해주는 코드가 작성이 되어 있었다. 마찬가지로, 500번대 에러의 경우에도...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.