Updated vs2005 projects for AllTypesOf<> registration updates.
[castle.git] / InversionOfControl / Castle.MicroKernel / Registration / Strategies / ServiceDescriptor.cs
blob55cf497037e4f3047a3a76e4279abab62db6f720
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.Registration
17 using System;
19 /// <summary>
20 /// Describes how to select a types service.
21 /// </summary>
22 /// <typeparam name="T">The base type to match against.</typeparam>
23 public class ServiceDescriptor<T>
25 public delegate Type ServiceSelector(Type type);
27 private readonly TypesDescriptor<T> typesDescriptor;
28 private ServiceSelector serviceSelector;
30 internal ServiceDescriptor(TypesDescriptor<T> typesDescriptor)
32 this.typesDescriptor = typesDescriptor;
35 /// <summary>
36 /// Uses the base type matched on.
37 /// </summary>
38 /// <returns></returns>
39 public TypesDescriptor<T> Base()
41 return Select(delegate(Type type)
43 return typeof(T);
44 });
47 /// <summary>
48 /// Uses the first interface of a type.
49 /// </summary>
50 /// <returns></returns>
51 public TypesDescriptor<T> FirstInterface()
53 return Select(delegate(Type type)
55 Type[] interfaces = type.GetInterfaces();
56 if (interfaces.Length > 0)
58 return interfaces[0];
60 return null;
61 });
64 /// <summary>
65 /// Assigns a custom service selection strategy.
66 /// </summary>
67 /// <param name="selector"></param>
68 /// <returns></returns>
69 public TypesDescriptor<T> Select(ServiceSelector selector)
71 this.serviceSelector = selector;
72 return typesDescriptor;
75 internal Type GetService(Type type)
77 if (serviceSelector != null)
79 return serviceSelector(type) ?? type;
81 return type;