forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationServices.cs
More file actions
67 lines (58 loc) · 2.63 KB
/
ConfigurationServices.cs
File metadata and controls
67 lines (58 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using ServiceStack.RazorEngine.Templating;
using System;
namespace ServiceStack.RazorEngine.Configuration
{
/// <summary>
/// Provides configuration service operations.
/// </summary>
public static class ConfigurationServices
{
#region Methods
/// <summary>
/// Adds any configured namespaces to the target template service.
/// </summary>
/// <param name="service">The template service.</param>
/// <param name="config">The namespace configuration element.</param>
public static void AddNamespaces(TemplateService service, NamespaceConfigurationElementCollection config)
{
foreach (NamespaceConfigurationElement @namespace in config)
{
service.Namespaces.Add(@namespace.Namespace);
}
}
/// <summary>
/// Creates an instance of the specified type.
/// </summary>
/// <typeparam name="T">The expected type.</typeparam>
/// <param name="typeName">The type name.</param>
/// <returns>An instance of the specified type.</returns>
public static T CreateInstance<T>(string typeName) where T : class
{
if (string.IsNullOrWhiteSpace(typeName))
throw new ArgumentException("Type name is required.");
Type type = Type.GetType(typeName);
if (type == null)
throw new ArgumentException("The type '" + typeName + "' could not be loaded.");
T instance = Activator.CreateInstance(type) as T;
if (instance == null)
throw new ArgumentException("The type '" + typeName + "' is not an instance of '" + typeof(T).FullName + "'.");
return instance;
}
/// <summary>
/// Creates an instance of <see cref="TemplateService"/> from the specified configuration.
/// </summary>
/// <param name="config">The template service configuration.</param>
/// <returns>An instance of <see cref="TemplateService"/>.</returns>
public static TemplateService CreateTemplateService(TemplateServiceConfigurationElement config)
{
if (config == null)
throw new ArgumentNullException("config");
var service = TemplateServiceFactory.CreateTemplateService(config);
AddNamespaces(service, config.Namespaces);
if (!string.IsNullOrWhiteSpace(config.Activator))
service.SetActivator(CreateInstance<IActivator>(config.Activator));
return service;
}
#endregion
}
}