Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy / DefaultProxyBuilder.cs
blob15cb9efd1bd2da7097a2015732a9640f4c937d4e
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 Castle.DynamicProxy.Generators;
21 public class DefaultProxyBuilder : IProxyBuilder
23 private readonly ModuleScope scope;
25 public DefaultProxyBuilder() : this(new ModuleScope())
29 public DefaultProxyBuilder(ModuleScope scope)
31 this.scope = scope;
34 public ModuleScope ModuleScope
36 get { return scope; }
39 public virtual Type CreateClassProxy(Type theClass, ProxyGenerationOptions options)
41 AssertValidType(theClass);
43 ClassProxyGenerator generator = new ClassProxyGenerator(scope, theClass);
45 return generator.GenerateCode(null, options);
48 public Type CreateClassProxy(Type theClass, Type[] interfaces, ProxyGenerationOptions options)
50 AssertValidType(theClass);
51 AssertValidTypes(interfaces);
53 ClassProxyGenerator generator = new ClassProxyGenerator(scope, theClass);
55 return generator.GenerateCode(interfaces, options);
58 public Type CreateInterfaceProxyTypeWithoutTarget(Type theInterface, Type[] interfaces, ProxyGenerationOptions options)
60 AssertValidType(theInterface);
61 AssertValidTypes(interfaces);
63 InterfaceProxyWithoutTargetGenerator generatorWithoutTarget =
64 new InterfaceProxyWithoutTargetGenerator(scope, theInterface);
66 return generatorWithoutTarget.GenerateCode(typeof(object), interfaces, options);
69 public Type CreateInterfaceProxyTypeWithTarget(Type theInterface, Type[] interfaces, Type targetType,
70 ProxyGenerationOptions options)
72 AssertValidType(theInterface);
73 AssertValidTypes(interfaces);
75 InterfaceProxyWithTargetGenerator generator = new InterfaceProxyWithTargetGenerator(scope, theInterface);
77 return generator.GenerateCode(targetType, interfaces, options);
80 public Type CreateInterfaceProxyTypeWithTargetInterface(Type theInterface, ProxyGenerationOptions options)
82 AssertValidType(theInterface);
84 InterfaceProxyWithTargetInterfaceGenerator generator =
85 new InterfaceProxyWithTargetInterfaceGenerator(scope, theInterface);
87 return generator.GenerateCode(theInterface, null, options);
90 private void AssertValidType(Type target)
92 bool isNestedAndInternal = target.IsNested && (target.IsNestedAssembly || target.IsNestedFamORAssem);
93 bool isInternalNotNested = target.IsVisible == false && target.IsNested == false;
94 bool internalAndVisibleToDynProxy = (isInternalNotNested || isNestedAndInternal) &&
95 InternalsHelper.IsInternalToDynamicProxy(target.Assembly);
96 if (!target.IsPublic && !target.IsNestedPublic && !internalAndVisibleToDynProxy)
98 throw new GeneratorException("Type is not public, so a proxy cannot be generated. Type: " + target.FullName);
100 if (target.IsGenericTypeDefinition)
102 throw new GeneratorException ("Type is a generic tyspe definition, so a proxy cannot be generated. Type: " + target.FullName);
106 private void AssertValidTypes(IEnumerable targetTypes)
108 if (targetTypes != null)
110 foreach(Type t in targetTypes)
112 AssertValidType(t);