Added container accessor to Castle.Core
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy / ProxyGenerator.cs
blobf61cc630051242866ab2dc2bb5379b30e95420f3
1 // Copyright 2004-2007 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 Castle.Core.Interceptor;
21 [CLSCompliant(true)]
22 public class ProxyGenerator
24 private readonly IProxyBuilder proxyBuilder;
26 #region Constructors
28 /// <summary>
29 /// Initializes a new instance of the <see cref="ProxyGenerator"/> class.
30 /// </summary>
31 /// <param name="builder">The builder.</param>
32 public ProxyGenerator(IProxyBuilder builder)
34 proxyBuilder = builder;
37 /// <summary>
38 /// Initializes a new instance of the <see cref="ProxyGenerator"/> class.
39 /// </summary>
40 public ProxyGenerator() : this(new DefaultProxyBuilder())
44 #endregion
46 #region Properties
48 /// <summary>
49 /// Gets the proxy builder instance.
50 /// </summary>
51 /// <value>The proxy builder.</value>
52 public IProxyBuilder ProxyBuilder
54 get { return proxyBuilder; }
57 #endregion
59 private void CheckNotGenericTypeDefinition(Type type, string argumentName)
61 if (type != null && type.IsGenericTypeDefinition)
63 throw new ArgumentException ("You can't specify a generic type definition.", argumentName);
67 private void CheckNotGenericTypeDefinitions(IEnumerable types, string argumentName)
69 if (types != null)
71 foreach (Type t in types)
73 CheckNotGenericTypeDefinition (t, argumentName);
78 #region CreateInterfaceProxyWithTarget
80 public T CreateInterfaceProxyWithTarget<T>(object target, params IInterceptor[] interceptors)
82 return (T) CreateInterfaceProxyWithTarget(typeof(T), target, ProxyGenerationOptions.Default, interceptors);
85 public T CreateInterfaceProxyWithTarget<T>(object target, ProxyGenerationOptions options,
86 params IInterceptor[] interceptors)
88 return (T) CreateInterfaceProxyWithTarget(typeof(T), target, options, interceptors);
91 public object CreateInterfaceProxyWithTarget(Type theInterface, object target, params IInterceptor[] interceptors)
93 return CreateInterfaceProxyWithTarget(theInterface, target, ProxyGenerationOptions.Default, interceptors);
96 public object CreateInterfaceProxyWithTarget(Type theInterface, object target, ProxyGenerationOptions options,
97 params IInterceptor[] interceptors)
99 return CreateInterfaceProxyWithTarget(theInterface, null, target, options, interceptors);
102 public object CreateInterfaceProxyWithTarget(Type theInterface, Type[] interfaces,
103 object target, params IInterceptor[] interceptors)
105 return CreateInterfaceProxyWithTarget(theInterface, interfaces, target, ProxyGenerationOptions.Default, interceptors);
108 public object CreateInterfaceProxyWithTarget(Type theInterface, Type[] interfaces, object target,
109 ProxyGenerationOptions options, params IInterceptor[] interceptors)
111 if (theInterface == null)
113 throw new ArgumentNullException("theInterface");
115 if (target == null)
117 throw new ArgumentNullException("target");
119 if (interceptors == null)
121 throw new ArgumentNullException("interceptors");
124 if (!theInterface.IsInterface)
126 throw new ArgumentException("Specified type is not an interface", "theInterface");
129 if (!theInterface.IsAssignableFrom(target.GetType()))
131 throw new ArgumentException("Target does not implement interface " + theInterface.FullName, "target");
134 CheckNotGenericTypeDefinition(theInterface, "theInterface");
135 CheckNotGenericTypeDefinitions(interfaces, "interfaces");
137 Type targetType = target.GetType();
138 Type generatedType = CreateInterfaceProxyTypeWithTarget(theInterface, interfaces, targetType, options);
140 return Activator.CreateInstance(generatedType, new object[] {interceptors, target});
143 #endregion
145 #region CreateInterfaceProxyWithTargetInterface
147 public object CreateInterfaceProxyWithTargetInterface(Type theInterface, object target,
148 params IInterceptor[] interceptors)
150 return CreateInterfaceProxyWithTargetInterface(theInterface, target, ProxyGenerationOptions.Default, interceptors);
153 public object CreateInterfaceProxyWithTargetInterface(Type theInterface, object target,
154 ProxyGenerationOptions options,
155 params IInterceptor[] interceptors)
157 if (!theInterface.IsInstanceOfType(target))
159 throw new ArgumentException("targetType");
161 if (theInterface == null)
163 throw new ArgumentNullException("theInterface");
165 if (target == null)
167 throw new ArgumentNullException("target");
169 if (interceptors == null)
171 throw new ArgumentNullException("interceptors");
174 if (!theInterface.IsInterface)
176 throw new ArgumentException("Specified type is not an interface", "theInterface");
179 if (!theInterface.IsAssignableFrom(target.GetType()))
181 throw new ArgumentException("Target does not implement interface " + theInterface.FullName, "target");
184 CheckNotGenericTypeDefinition(theInterface, "theInterface");
186 Type generatedType = CreateInterfaceProxyTypeWithTargetInterface(theInterface, null, theInterface, options);
187 return Activator.CreateInstance(generatedType, new object[] {interceptors, target});
190 #endregion
192 #region CreateInterfaceProxyWithoutTarget
194 public T CreateInterfaceProxyWithoutTarget<T>(IInterceptor interceptor)
196 return (T) CreateInterfaceProxyWithoutTarget(typeof(T), interceptor);
199 public T CreateInterfaceProxyWithoutTarget<T>(params IInterceptor[] interceptors)
201 return (T) CreateInterfaceProxyWithoutTarget(typeof(T), interceptors);
204 public object CreateInterfaceProxyWithoutTarget(Type theInterface, IInterceptor interceptor)
206 return CreateInterfaceProxyWithoutTarget(theInterface, new Type[0],
207 ProxyGenerationOptions.Default,
208 interceptor);
211 public object CreateInterfaceProxyWithoutTarget(Type theInterface, params IInterceptor[] interceptors)
213 return CreateInterfaceProxyWithoutTarget(theInterface, new Type[0], ProxyGenerationOptions.Default, interceptors);
216 public object CreateInterfaceProxyWithoutTarget(Type theInterface, Type[] interfaces,
217 params IInterceptor[] interceptors)
219 return CreateInterfaceProxyWithoutTarget(theInterface, interfaces, ProxyGenerationOptions.Default, interceptors);
222 public object CreateInterfaceProxyWithoutTarget(Type theInterface, Type[] interfaces, ProxyGenerationOptions options,
223 params IInterceptor[] interceptors)
225 if (theInterface == null)
227 throw new ArgumentNullException("theInterface");
229 if (interceptors == null)
231 throw new ArgumentNullException("interceptors");
234 if (!theInterface.IsInterface)
236 throw new ArgumentException("Specified type is not an interface", "theInterface");
239 CheckNotGenericTypeDefinition(theInterface, "theInterface");
240 CheckNotGenericTypeDefinitions(interfaces, "interfaces");
242 Type generatedType = CreateInterfaceProxyTypeWithoutTarget(theInterface, interfaces, options);
243 return Activator.CreateInstance(generatedType, new object[] {interceptors, new object(),});
246 #endregion
248 #region CreateClassProxy
250 public T CreateClassProxy<T>(params IInterceptor[] interceptors)
252 return (T) CreateClassProxy(typeof(T), ProxyGenerationOptions.Default, interceptors);
255 public object CreateClassProxy(Type targetType, params IInterceptor[] interceptors)
257 return CreateClassProxy(targetType, ProxyGenerationOptions.Default, interceptors);
260 /// <summary>
261 /// Creates the class proxy.
262 /// </summary>
263 /// <param name="targetType">Type of the target.</param>
264 /// <param name="interfaces">The interfaces.</param>
265 /// <param name="interceptors">The interceptors.</param>
266 /// <returns></returns>
267 public object CreateClassProxy(Type targetType, Type[] interfaces, params IInterceptor[] interceptors)
269 return CreateClassProxy(targetType, interfaces, ProxyGenerationOptions.Default, interceptors);
272 /// <summary>
273 /// Creates the class proxy.
274 /// </summary>
275 /// <param name="targetType">Type of the target.</param>
276 /// <param name="interceptors">The interceptors.</param>
277 /// <param name="constructorArgs">The constructor args.</param>
278 /// <returns></returns>
279 public object CreateClassProxy(Type targetType, IInterceptor[] interceptors,
280 params object[] constructorArgs)
282 return CreateClassProxy(targetType, null, ProxyGenerationOptions.Default,
283 constructorArgs, interceptors);
286 /// <summary>
287 ///
288 /// </summary>
289 /// <param name="targetType"></param>
290 /// <param name="options"></param>
291 /// <param name="interceptors"></param>
292 /// <returns></returns>
293 public object CreateClassProxy(Type targetType, ProxyGenerationOptions options, params IInterceptor[] interceptors)
295 return CreateClassProxy(targetType, null, options, interceptors);
298 public object CreateClassProxy(Type targetType, Type[] interfaces,
299 ProxyGenerationOptions options, params IInterceptor[] interceptors)
301 return CreateClassProxy(targetType, interfaces, options, null, interceptors);
304 /// <summary>
305 /// Creates the class proxy.
306 /// </summary>
307 /// <param name="targetType">Type of the target.</param>
308 /// <param name="interfaces">The interfaces.</param>
309 /// <param name="options">The options.</param>
310 /// <param name="constructorArgs">The constructor args.</param>
311 /// <param name="interceptors">The interceptors.</param>
312 /// <returns></returns>
313 public object CreateClassProxy(Type targetType, Type[] interfaces, ProxyGenerationOptions options,
314 object[] constructorArgs, params IInterceptor[] interceptors)
316 if (targetType == null)
318 throw new ArgumentNullException("targetType");
320 if (options == null)
322 throw new ArgumentNullException("options");
324 if (!targetType.IsClass)
326 throw new ArgumentException("'targetType' must be a class", "targetType");
329 CheckNotGenericTypeDefinition(targetType, "targetType");
330 CheckNotGenericTypeDefinitions(interfaces, "interfaces");
332 Type proxyType = CreateClassProxyType(targetType, interfaces, options);
334 object[] args;
336 if (constructorArgs != null && constructorArgs.Length != 0)
338 args = new object[constructorArgs.Length + 1];
339 args[0] = interceptors;
341 Array.Copy(constructorArgs, 0, args, 1, constructorArgs.Length);
343 else
345 args = new object[] {interceptors};
348 return Activator.CreateInstance(proxyType, args);
351 #endregion
353 protected Type CreateClassProxyType(Type baseClass, Type[] interfaces, ProxyGenerationOptions options)
355 return ProxyBuilder.CreateClassProxy(baseClass, interfaces, options);
358 protected Type CreateInterfaceProxyTypeWithTarget(Type theInterface, Type[] interfaces, Type targetType,
359 ProxyGenerationOptions options)
361 return ProxyBuilder.CreateInterfaceProxyTypeWithTarget(theInterface, interfaces, targetType, options);
364 protected Type CreateInterfaceProxyTypeWithTargetInterface(Type theInterface, Type[] interfaces, Type targetType,
365 ProxyGenerationOptions options)
367 return ProxyBuilder.CreateInterfaceProxyTypeWithTargetInterface(theInterface, options);
370 protected Type CreateInterfaceProxyTypeWithoutTarget(Type theInterface, Type[] interfaces,
371 ProxyGenerationOptions options)
373 return ProxyBuilder.CreateInterfaceProxyTypeWithoutTarget(theInterface, interfaces, options);