Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Providers / DefaultViewComponentDescriptorProvider.cs
blob3144e833e6bff154e62316332814034169fc661f
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.MonoRail.Framework.Providers
17 using System;
18 using System.Collections.Generic;
19 using Castle.MonoRail.Framework;
20 using Descriptors;
21 using ViewComponents;
23 /// <summary>
24 /// Creates <see cref="ViewComponentDescriptor"/> from attributes
25 /// associated with the <see cref="ViewComponent"/>
26 /// </summary>
27 public class DefaultViewComponentDescriptorProvider : IViewComponentDescriptorProvider
29 private readonly IDictionary<Type, ViewComponentDescriptor> type2Desc =
30 new Dictionary<Type, ViewComponentDescriptor>();
32 /// <summary>
33 /// Services the specified provider.
34 /// </summary>
35 /// <param name="provider">The provider.</param>
36 public void Service(IMonoRailServices provider)
40 /// <summary>
41 /// Creates a <see cref="ViewComponentDescriptor"/> by inspecting the
42 /// specified view component type.
43 /// </summary>
44 /// <param name="viewComponentType">Type of the view component.</param>
45 /// <returns></returns>
46 public ViewComponentDescriptor Collect(Type viewComponentType)
48 if (viewComponentType == null) throw new ArgumentNullException("viewComponentType");
50 if (type2Desc.ContainsKey(viewComponentType))
52 return type2Desc[viewComponentType];
55 object[] attrs = viewComponentType.GetCustomAttributes(typeof(ViewComponentDetailsAttribute), true);
57 ViewComponentDescriptor descriptor;
59 if (attrs.Length == 0)
61 descriptor = ViewComponentDescriptor.Empty;
63 else
65 ViewComponentDetailsAttribute details = (ViewComponentDetailsAttribute) attrs[0];
67 IViewComponentCacheKeyGenerator generator = null;
69 if (details.Cache == ViewComponentCache.Always)
71 generator = new AlwaysCacheKeyGenerator();
73 else if (details.CacheKeyFactory != null)
75 try
77 generator = (IViewComponentCacheKeyGenerator)
78 Activator.CreateInstance(details.CacheKeyFactory);
80 catch(Exception ex)
82 throw new MonoRailException(
83 "Could not instantiate IViewComponentCacheKeyGenerator implementation or " +
84 "it does not implement this interface. Type: " + details.CacheKeyFactory.FullName, ex);
88 descriptor = new ViewComponentDescriptor(details.Cache != ViewComponentCache.Disabled, details.Cache, generator);
91 type2Desc[viewComponentType] = descriptor;
93 return descriptor;