Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy / ProxyGenerationOptions.cs
blob9f648f6995a7e875222999757ace10d372b86126
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.DynamicProxy
17 using System;
18 using System.Collections;
19 using System.Runtime.Serialization;
21 [Serializable]
22 public class ProxyGenerationOptions : ISerializable
24 public static readonly ProxyGenerationOptions Default = new ProxyGenerationOptions();
26 private IProxyGenerationHook hook;
27 private IInterceptorSelector selector;
28 private ArrayList mixins;
29 private Type baseTypeForInterfaceProxy = typeof(object);
30 private bool useSingleInterfaceProxy;
31 private bool useSelector;
33 /// <summary>
34 /// Initializes a new instance of the <see cref="ProxyGenerationOptions"/> class.
35 /// </summary>
36 /// <param name="hook">The hook.</param>
37 public ProxyGenerationOptions(IProxyGenerationHook hook)
39 this.hook = hook;
40 useSelector = false;
43 /// <summary>
44 /// Initializes a new instance of the <see cref="ProxyGenerationOptions"/> class.
45 /// </summary>
46 public ProxyGenerationOptions() : this(new AllMethodsHook())
50 private ProxyGenerationOptions (SerializationInfo info, StreamingContext context)
52 hook = (IProxyGenerationHook) info.GetValue ("hook", typeof (IProxyGenerationHook));
53 selector = (IInterceptorSelector) info.GetValue ("selector", typeof (IInterceptorSelector));
54 mixins = (ArrayList) info.GetValue ("mixins", typeof (ArrayList));
55 baseTypeForInterfaceProxy = Type.GetType (info.GetString ("baseTypeForInterfaceProxy.AssemblyQualifiedName"));
56 useSingleInterfaceProxy = info.GetBoolean ("useSingleInterfaceProxy");
57 useSelector = info.GetBoolean ("useSelector");
60 public void GetObjectData (SerializationInfo info, StreamingContext context)
62 info.AddValue ("hook", hook);
63 info.AddValue ("selector", selector);
64 info.AddValue ("mixins", mixins);
65 info.AddValue ("baseTypeForInterfaceProxy.AssemblyQualifiedName", baseTypeForInterfaceProxy.AssemblyQualifiedName);
66 info.AddValue ("useSingleInterfaceProxy", useSingleInterfaceProxy);
67 info.AddValue ("useSelector", useSelector);
70 public IProxyGenerationHook Hook
72 get { return hook; }
73 set { hook = value; }
76 public IInterceptorSelector Selector
78 get { return selector; }
79 set { selector = value; }
82 public bool UseSelector
84 get { return useSelector; }
85 set { useSelector = value; }
88 public bool UseSingleInterfaceProxy
90 get { return useSingleInterfaceProxy; }
91 set { useSingleInterfaceProxy = value; }
94 public void AddMixinInstance(object instance)
96 if (instance == null)
98 throw new ArgumentNullException("instance");
101 if (mixins == null)
103 mixins = new ArrayList();
106 mixins.Add(instance);
109 public object[] MixinsAsArray()
111 if (mixins == null) return new object[0];
113 return mixins.ToArray();
116 public bool HasMixins
118 get { return mixins == null ? false : mixins.Count != 0; }
121 public Type BaseTypeForInterfaceProxy
123 get { return baseTypeForInterfaceProxy; }
124 set { baseTypeForInterfaceProxy = value; }
127 public override bool Equals(object obj)
129 if (ReferenceEquals (this, obj)) return true;
130 ProxyGenerationOptions proxyGenerationOptions = obj as ProxyGenerationOptions;
131 if (ReferenceEquals (proxyGenerationOptions, null)) return false;
133 if (!Equals(hook, proxyGenerationOptions.hook)) return false;
134 if (!Equals(selector, proxyGenerationOptions.selector)) return false;
135 if (!ListEquals(mixins, proxyGenerationOptions.mixins)) return false;
136 if (!Equals(baseTypeForInterfaceProxy, proxyGenerationOptions.baseTypeForInterfaceProxy)) return false;
137 if (!Equals(useSingleInterfaceProxy, proxyGenerationOptions.useSingleInterfaceProxy)) return false;
138 if (!Equals (useSelector, proxyGenerationOptions.useSelector))
139 return false;
140 return true;
143 public override int GetHashCode ()
145 int result = hook != null ? hook.GetType().GetHashCode() : 0;
146 result = 29 * result + (selector != null ? selector.GetHashCode() : 0);
147 result = 29 * result + (mixins != null ? GetListHashCode (mixins) : 0);
148 result = 29 * result + (baseTypeForInterfaceProxy != null ? baseTypeForInterfaceProxy.GetHashCode() : 0);
149 result = 29 * result + useSingleInterfaceProxy.GetHashCode();
150 result = 29 * result + useSelector.GetHashCode ();
151 return result;
154 private static bool ListEquals (IList one, IList two)
156 if (one == two)
157 return true;
158 if (one == null || two == null)
159 return false;
161 if (one.Count != two.Count)
162 return false;
164 for (int i = 0; i < one.Count; ++i)
166 if (!object.Equals (one[i], two[i]))
167 return false;
170 return true;
173 private static int GetListHashCode (IList list)
175 if (list == null)
176 return 0;
178 int hashCode = 0;
179 foreach (object o in list)
180 hashCode = 29 * hashCode + (o != null ? o.GetHashCode () : 0);
182 return hashCode;