1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
.MicroKernel
.Proxy
18 using System
.Collections
;
21 /// Represents options to configure proxies.
23 public class ProxyOptions
25 private IProxyHook hook
;
26 private ArrayList interfaceList
;
27 private bool useSingleInterfaceProxy
;
28 private bool useMarshalByRefAsBaseClass
;
29 private bool allowChangeTarget
;
30 private bool omitTarget
;
33 /// Initializes a new instance of the <see cref="ProxyOptions"/> class.
37 useSingleInterfaceProxy
= false;
42 /// Gets or sets the proxy hook.
44 public IProxyHook Hook
51 /// Determines if the proxied component uses a target.
53 public bool OmitTarget
55 get { return omitTarget; }
56 set { omitTarget = value; }
60 /// Determines if the proxied component can change targets.
62 public bool AllowChangeTarget
64 get { return allowChangeTarget; }
65 set { allowChangeTarget = value; }
69 /// Determines if the proxied component should only include
70 /// the service interface.
72 public bool UseSingleInterfaceProxy
74 get { return useSingleInterfaceProxy; }
75 set { useSingleInterfaceProxy = value; }
79 /// Determines if the interface proxied component should inherit
80 /// from <see cref="MarshalByRefObject"/>
82 public bool UseMarshalByRefAsBaseClass
84 get { return useMarshalByRefAsBaseClass; }
85 set { useMarshalByRefAsBaseClass = value; }
89 /// Gets the additional interfaces to proxy.
91 /// <value>The interfaces.</value>
92 public Type
[] AdditionalInterfaces
96 if (interfaceList
!= null)
98 return (Type
[]) interfaceList
.ToArray(typeof(Type
));
106 /// Adds the additional interfaces to proxy.
108 /// <param name="interfaces">The interfaces.</param>
109 public void AddAdditionalInterfaces(params Type
[] interfaces
)
111 if (interfaces
== null)
113 throw new ArgumentNullException("interfaces");
116 if (interfaceList
== null)
118 interfaceList
= new ArrayList();
121 interfaceList
.AddRange(interfaces
);
125 /// Equalses the specified obj.
127 /// <param name="obj">The obj.</param>
128 /// <returns>true if equal.</returns>
129 public override bool Equals(object obj
)
131 if (this == obj
) return true;
132 ProxyOptions proxyOptions
= obj
as ProxyOptions
;
133 if (proxyOptions
== null) return false;
134 if (!Equals(hook
, proxyOptions
.hook
)) return false;
135 if (!Equals(useSingleInterfaceProxy
, proxyOptions
.useSingleInterfaceProxy
)) return false;
136 if (!Equals(omitTarget
, proxyOptions
.omitTarget
)) return false;
137 return AdditionalInterfacesAreEquals(proxyOptions
);
141 /// Gets the hash code.
143 /// <returns></returns>
144 public override int GetHashCode()
146 return 29 * base.GetHashCode() + GetAdditionalInterfacesHashCode();
149 private bool AdditionalInterfacesAreEquals(ProxyOptions proxyOptions
)
151 if (!Equals(interfaceList
== null, proxyOptions
.interfaceList
== null)) return false;
152 if (interfaceList
== null) return true; //both are null, nothing more to check
153 if (interfaceList
.Count
!= proxyOptions
.interfaceList
.Count
) return false;
154 for(int i
= 0; i
< interfaceList
.Count
; ++i
)
156 if (!proxyOptions
.interfaceList
.Contains(interfaceList
[0])) return false;
161 private int GetAdditionalInterfacesHashCode()
165 if (interfaceList
== null) return result
;
167 foreach(object type
in interfaceList
)
169 result
= 29 * result
+ type
.GetHashCode();