// 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 { /// /// Describes script template generator. /// public sealed class ScriptGeneratorDescriptor { private static List s_Descriptors; private static ReadOnlyCollection s_DescriptorsReadOnly; static ScriptGeneratorDescriptor() { // Gather script template generator types. s_Descriptors = new List(); 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(s_Descriptors); } /// /// Gets read-only collection of script template descriptors. /// public static IList Descriptors { get { return s_DescriptorsReadOnly; } } /// /// Type of template generator. /// public Type Type { get; private set; } /// /// Associated attribute which includes description of template generator. /// public ScriptTemplateAttribute Attribute { get; private set; } /// /// Initialize new instance. /// private ScriptGeneratorDescriptor() { } /// /// Create new instance of script generator. /// /// /// The new instance. /// public ScriptTemplateGenerator CreateInstance() { return Activator.CreateInstance(Type) as ScriptTemplateGenerator; } } }