1
// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
20 /// Describes how to select a types service.
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
;
36 /// Uses the base type matched on.
38 /// <returns></returns>
39 public TypesDescriptor
<T
> Base()
41 return Select(delegate(Type type
)
48 /// Uses the first interface of a type.
50 /// <returns></returns>
51 public TypesDescriptor
<T
> FirstInterface()
53 return Select(delegate(Type type
)
55 Type
[] interfaces
= type
.GetInterfaces();
56 if (interfaces
.Length
> 0)
65 /// Assigns a custom service selection strategy.
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
;