Applied patch from Jan Limpens 'ReflectionBasedDictionaryAdapter needs to check if...
[castle.git] / Core / Castle.Core / Model / InterceptorReference.cs
blobadcdb2a30ffc35f54d5751888f0b10392d6e7fa9
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Core
17 using System;
19 public enum InterceptorReferenceType
21 Interface,
22 Key
25 /// <summary>
26 /// Represents an reference to a Interceptor component.
27 /// </summary>
28 #if !SILVERLIGHT
29 [Serializable]
30 #endif
31 public class InterceptorReference : IEquatable<InterceptorReference>
33 private readonly InterceptorReferenceType refType;
34 private readonly Type serviceType;
35 private readonly String componentKey;
37 /// <summary>
38 /// Initializes a new instance of the <see cref="InterceptorReference"/> class.
39 /// </summary>
40 /// <param name="componentKey">The component key.</param>
41 public InterceptorReference(String componentKey)
43 if (componentKey == null)
45 throw new ArgumentNullException("componentKey cannot be null");
48 refType = InterceptorReferenceType.Key;
49 this.componentKey = componentKey;
52 /// <summary>
53 /// Initializes a new instance of the <see cref="InterceptorReference"/> class.
54 /// </summary>
55 /// <param name="serviceType">Type of the service.</param>
56 public InterceptorReference(Type serviceType)
58 if (serviceType == null)
60 throw new ArgumentNullException("'serviceType' cannot be null");
63 refType = InterceptorReferenceType.Interface;
64 this.serviceType = serviceType;
67 /// <summary>
68 /// Gets the type of the service.
69 /// </summary>
70 /// <value>The type of the service.</value>
71 public Type ServiceType
73 get { return serviceType; }
76 /// <summary>
77 /// Gets the interceptor component key.
78 /// </summary>
79 /// <value>The component key.</value>
80 public String ComponentKey
82 get { return componentKey; }
85 /// <summary>
86 /// Gets the type of the reference.
87 /// </summary>
88 /// <value>The type of the reference.</value>
89 public InterceptorReferenceType ReferenceType
91 get { return refType; }
94 /// <summary>
95 /// Gets an <see cref="InterceptorReference"/> for the component key.
96 /// </summary>
97 /// <param name="key">The component key.</param>
98 /// <returns>The <see cref="InterceptorReference"/></returns>
99 public static InterceptorReference ForKey(String key)
101 return new InterceptorReference(key);
104 /// <summary>
105 /// Gets an <see cref="InterceptorReference"/> for the service.
106 /// </summary>
107 /// <param name="service">The service.</param>
108 /// <returns>The <see cref="InterceptorReference"/></returns>
109 public static InterceptorReference ForType(Type service)
111 return new InterceptorReference(service);
114 /// <summary>
115 /// Gets an <see cref="InterceptorReference"/> for the service.
116 /// </summary>
117 /// <typeparam name="T">The service type.</typeparam>
118 /// <returns>The <see cref="InterceptorReference"/></returns>
119 public static InterceptorReference ForType<T>()
121 return new InterceptorReference(typeof(T));
124 public bool Equals(InterceptorReference interceptorReference)
126 if (interceptorReference == null) return false;
127 if (!Equals(refType, interceptorReference.refType)) return false;
128 if (!Equals(serviceType, interceptorReference.serviceType)) return false;
129 if (!Equals(componentKey, interceptorReference.componentKey)) return false;
130 return true;
133 public override bool Equals(object obj)
135 if (ReferenceEquals(this, obj)) return true;
136 return Equals(obj as InterceptorReference);
139 public override int GetHashCode()
141 int result = refType.GetHashCode();
142 result = 29*result + (serviceType != null ? serviceType.GetHashCode() : 0);
143 result = 29*result + (componentKey != null ? componentKey.GetHashCode() : 0);
144 return result;