aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Extensibility/BaseExtensionFactory.cs
blob: 2e21b51be577bd6d3e5a364707b5820b8cbd4a77 (plain)
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
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Extensibility
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Base class for extension factories.
    ///
    /// Implementations may request an IWixToolsetCoreServiceProvider at instantiation by having a single parameter constructor for it.
    /// </summary>
    public abstract class BaseExtensionFactory : IExtensionFactory
    {
        /// <summary>
        /// The extension types of the WiX extension.
        /// </summary>
        protected abstract IEnumerable<Type> ExtensionTypes { get; }

        /// <summary>
        /// See <see cref="IExtensionFactory.TryCreateExtension(Type, out object)"/>
        /// </summary>
        public virtual bool TryCreateExtension(Type extensionType, out object extension)
        {
            extension = null;

            foreach (var type in this.ExtensionTypes)
            {
                if (extensionType.IsAssignableFrom(type))
                {
                    extension = Activator.CreateInstance(type);
                    break;
                }
            }

            return extension != null;
        }
    }
}