-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptGeneratorDescriptor.cs
More file actions
71 lines (58 loc) · 2.24 KB
/
Copy pathScriptGeneratorDescriptor.cs
File metadata and controls
71 lines (58 loc) · 2.24 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
68
69
70
71
// Copyright (c) Rotorz Limited. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace ScriptTemplates {
/// <summary>
/// Describes script template generator.
/// </summary>
public sealed class ScriptGeneratorDescriptor {
private static List<ScriptGeneratorDescriptor> s_Descriptors;
private static ReadOnlyCollection<ScriptGeneratorDescriptor> s_DescriptorsReadOnly;
static ScriptGeneratorDescriptor() {
// Gather script template generator types.
s_Descriptors = new List<ScriptGeneratorDescriptor>();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
foreach (var type in assembly.GetTypes())
if (type.IsDefined(typeof(ScriptTemplateAttribute), true))
s_Descriptors.Add(new ScriptGeneratorDescriptor() {
Type = type,
Attribute = (ScriptTemplateAttribute)type.GetCustomAttributes(typeof(ScriptTemplateAttribute), true).First()
});
// Sort descriptor by priority!
s_Descriptors.Sort((a, b) => a.Attribute.Priority - b.Attribute.Priority);
// We only want to expose read-only access to the collection ;)
s_DescriptorsReadOnly = new ReadOnlyCollection<ScriptGeneratorDescriptor>(s_Descriptors);
}
/// <summary>
/// Gets read-only collection of script template descriptors.
/// </summary>
public static IList<ScriptGeneratorDescriptor> Descriptors {
get { return s_DescriptorsReadOnly; }
}
/// <summary>
/// Type of template generator.
/// </summary>
public Type Type { get; private set; }
/// <summary>
/// Associated attribute which includes description of template generator.
/// </summary>
public ScriptTemplateAttribute Attribute { get; private set; }
/// <summary>
/// Initialize new <see cref="ScriptGeneratorDescriptor"/> instance.
/// </summary>
private ScriptGeneratorDescriptor() {
}
/// <summary>
/// Create new instance of script generator.
/// </summary>
/// <returns>
/// The new <see cref="ScriptTemplateGenerator"/> instance.
/// </returns>
public ScriptTemplateGenerator CreateInstance() {
return Activator.CreateInstance(Type) as ScriptTemplateGenerator;
}
}
}