Fixing an issue with output parameters that are of type IntPtr
[castle.git] / InversionOfControl / Castle.MicroKernel / ModelBuilder / Inspectors / ComponentActivatorInspector.cs
blob40e8d4b3ddb5cbaf62523dc1bbe6ba88cb146b2c
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.MicroKernel.ModelBuilder.Inspectors
17 using System;
18 using System.Configuration;
19 using Castle.Core;
21 /// <summary>
22 /// Inspects the component configuration and the type looking for a
23 /// definition of component activator type. The configuration preceeds whatever
24 /// is defined in the component.
25 /// </summary>
26 /// <remarks>
27 /// This inspector is not guarantee to always set up an component activator type.
28 /// If nothing could be found it wont touch the model. In this case is up to
29 /// the kernel to establish a default component activator for components.
30 /// </remarks>
31 [Serializable]
32 public class ComponentActivatorInspector : IContributeComponentModelConstruction
34 /// <summary>
35 /// Seaches for the component activator in the configuration and, if unsuccessful
36 /// look for the component activator attribute in the implementation type.
37 /// </summary>
38 /// <param name="kernel">The kernel instance</param>
39 /// <param name="model">The model instance</param>
40 public virtual void ProcessModel(IKernel kernel, ComponentModel model)
42 if (!ReadComponentActivatorFromConfiguration(model))
44 ReadComponentActivatorFromType(model);
48 /// <summary>
49 /// Reads the attribute "componentActivatorType" associated with the
50 /// component configuration and verifies it implements the <see cref="IComponentActivator"/>
51 /// interface.
52 /// </summary>
53 /// <exception cref="System.Configuration.ConfigurationException">
54 /// If the type does not implement the proper interface
55 /// </exception>
56 /// <param name="model"></param>
57 /// <returns></returns>
58 protected virtual bool ReadComponentActivatorFromConfiguration(ComponentModel model)
60 if (model.Configuration != null)
62 string componentActivatorType = model.Configuration.Attributes["componentActivatorType"];
64 if (componentActivatorType == null)
66 return false;
69 try
71 Type customComponentActivator = Type.GetType(componentActivatorType, true, false);
73 ValidateComponentActivator(customComponentActivator);
75 model.CustomComponentActivator = customComponentActivator;
77 catch(Exception ex)
79 string message =
80 String.Format("The Type '{0}' specified in the componentActivatorType attribute could not be loaded.",
81 componentActivatorType);
83 throw new ConfigurationErrorsException(message, ex);
87 return false;
90 /// <summary>
91 /// Check if the type expose one of the component activator attributes
92 /// defined in Castle.Core namespace.
93 /// </summary>
94 /// <param name="model"></param>
95 protected virtual void ReadComponentActivatorFromType(ComponentModel model)
97 object[] attributes = model.Implementation.GetCustomAttributes(typeof(ComponentActivatorAttribute), true);
99 if (attributes.Length != 0)
101 ComponentActivatorAttribute attribute = (ComponentActivatorAttribute) attributes[0];
103 ValidateComponentActivator(attribute.ComponentActivatorType);
105 model.CustomComponentActivator = attribute.ComponentActivatorType;
109 /// <summary>
110 /// Validates that the provide type implements IComponentActivator
111 /// </summary>
112 /// <param name="customComponentActivator">The custom component activator.</param>
113 protected virtual void ValidateComponentActivator(Type customComponentActivator)
115 if (!typeof(IComponentActivator).IsAssignableFrom(customComponentActivator))
117 string message =
118 String.Format(
119 "The Type '{0}' specified in the componentActivatorType attribute must implement Castle.MicroKernel.IComponentActivator",
120 customComponentActivator.FullName);
121 throw new InvalidOperationException(message);