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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
using Tango.Editors;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
/// <exclude/>
/// <summary>
/// A collection of <see cref="IConfiguration"/> extension methods.
/// </summary>
public static class IConfigurationExtensions
{
/// <summary>
/// Creates a new instance of the configurable.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="config">The configuration.</param>
/// <returns></returns>
public static T CreateConfigurable<T>(this IConfiguration config) where T : class
{
return CreateConfigurable<T>(config, new ConfigurationAnimation());
}
/// <summary>
/// Creates a new instance of the exact type provided and apply the configuration.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="config">The configuration.</param>
/// <returns></returns>
public static T CreateConfigurableForceType<T>(this IConfiguration config) where T : class
{
return CreateConfigurableForceType<T>(config, new ConfigurationAnimation());
}
/// <summary>
/// Creates a new instance of the configurable.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="config">The configuration.</param>
/// <param name="animation">The animation configuration.</param>
/// <returns></returns>
public static T CreateConfigurable<T>(this IConfiguration config, ConfigurationAnimation animation) where T : class
{
IConfigurable instance = Activator.CreateInstance(config.ConfigurableType) as IConfigurable;
instance.SetConfiguration(config, animation);
return instance as T;
}
/// <summary>
/// Creates a new instance of the configurable.
/// </summary>
/// <param name="config">The configuration.</param>
/// <returns></returns>
public static object CreateConfigurable(this IConfiguration config)
{
return CreateConfigurable(config, new ConfigurationAnimation());
}
/// <summary>
/// Creates a new instance of the configurable.
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="animation">The animation.</param>
/// <returns></returns>
public static object CreateConfigurable(this IConfiguration config, ConfigurationAnimation animation)
{
IConfigurable instance = Activator.CreateInstance(config.ConfigurableType) as IConfigurable;
instance.SetConfiguration(config, animation);
return instance;
}
/// <summary>
/// Creates a new instance of the exact type provided and apply the configuration.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="config">The configuration.</param>
/// <param name="animation">The animation.</param>
/// <returns></returns>
public static T CreateConfigurableForceType<T>(this IConfiguration config, ConfigurationAnimation animation) where T : class
{
IConfigurable instance = Activator.CreateInstance<T>() as IConfigurable;
instance.SetConfiguration(config, animation);
return instance as T;
}
/// <summary>
/// Saves the configuration as a file.
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="filePath">The file path.</param>
public static void SaveToFile(this IConfiguration config, String filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
BinaryFormatter f = new BinaryFormatter();
f.Serialize(fs, config);
}
}
/// <summary>
/// Saves to configuration to a stream.
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="stream">The stream.</param>
public static void SaveToStream(this IConfiguration config, Stream stream)
{
BinaryFormatter f = new BinaryFormatter();
f.Serialize(stream, config);
}
}
|