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 omitTarget
;
32 /// Initializes a new instance of the <see cref="ProxyOptions"/> class.
36 useSingleInterfaceProxy
= false;
41 /// Gets or sets the proxy hook.
43 public IProxyHook Hook
50 /// Determines if the proxied component uses a target.
52 public bool OmitTarget
54 get { return omitTarget; }
55 set { omitTarget = value; }
59 /// Determines if the proxied component should only include
60 /// the service interface.
62 public bool UseSingleInterfaceProxy
64 get { return useSingleInterfaceProxy; }
65 set { useSingleInterfaceProxy = value; }
69 /// Determines if the interface proxied component should inherit
70 /// from <see cref="MarshalByRefObject"/>
72 public bool UseMarshalByRefAsBaseClass
74 get { return useMarshalByRefAsBaseClass; }
75 set { useMarshalByRefAsBaseClass = value; }
79 /// Gets the additional interfaces to proxy.
81 /// <value>The interfaces.</value>
82 public Type
[] AdditionalInterfaces
86 if (interfaceList
!= null)
88 return (Type
[]) interfaceList
.ToArray(typeof(Type
));
96 /// Adds the additional interfaces to proxy.
98 /// <param name="interfaces">The interfaces.</param>
99 public void AddAdditionalInterfaces(params Type
[] interfaces
)
101 if (interfaces
== null)
103 throw new ArgumentNullException("interfaces");
106 if (interfaceList
== null)
108 interfaceList
= new ArrayList();
111 interfaceList
.AddRange(interfaces
);
115 /// Equalses the specified obj.
117 /// <param name="obj">The obj.</param>
118 /// <returns>true if equal.</returns>
119 public override bool Equals(object obj
)
121 if (this == obj
) return true;
122 ProxyOptions proxyOptions
= obj
as ProxyOptions
;
123 if (proxyOptions
== null) return false;
124 if (!Equals(hook
, proxyOptions
.hook
)) return false;
125 if (!Equals(useSingleInterfaceProxy
, proxyOptions
.useSingleInterfaceProxy
)) return false;
126 if (!Equals(omitTarget
, proxyOptions
.omitTarget
)) return false;
127 return AdditionalInterfacesAreEquals(proxyOptions
);
131 /// Gets the hash code.
133 /// <returns></returns>
134 public override int GetHashCode()
136 return 29 * base.GetHashCode() + GetAdditionalInterfacesHashCode();
139 private bool AdditionalInterfacesAreEquals(ProxyOptions proxyOptions
)
141 if (!Equals(interfaceList
== null, proxyOptions
.interfaceList
== null)) return false;
142 if (interfaceList
== null) return true; //both are null, nothing more to check
143 if (interfaceList
.Count
!= proxyOptions
.interfaceList
.Count
) return false;
144 for(int i
= 0; i
< interfaceList
.Count
; ++i
)
146 if (!proxyOptions
.interfaceList
.Contains(interfaceList
[0])) return false;
151 private int GetAdditionalInterfacesHashCode()
155 if (interfaceList
== null) return result
;
157 foreach(object type
in interfaceList
)
159 result
= 29 * result
+ type
.GetHashCode();