This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
ApplicationPartFactory.cs
62 lines (56 loc) · 2.68 KB
/
ApplicationPartFactory.cs
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Core;
namespace Microsoft.AspNetCore.Mvc.ApplicationParts
{
/// <summary>
/// Specifies a contract for synthesizing one or more <see cref="ApplicationPart"/> instances
/// from an <see cref="Assembly"/>.
/// <para>
/// By default, Mvc registers each application assembly that it discovers as an <see cref="AssemblyPart"/>.
/// Assemblies can optionally specify an <see cref="ApplicationPartFactory"/> to configure parts for the assembly
/// by using <see cref="ProvideApplicationPartFactoryAttribute"/>.
/// </para>
/// </summary>
public abstract class ApplicationPartFactory
{
/// <summary>
/// Gets one or more <see cref="ApplicationPart"/> instances for the specified <paramref name="assembly"/>.
/// </summary>
/// <param name="assembly">The <see cref="Assembly"/>.</param>
public abstract IEnumerable<ApplicationPart> GetApplicationParts(Assembly assembly);
/// <summary>
/// Gets the <see cref="ApplicationPartFactory"/> for the specified assembly.
/// <para>
/// An assembly may specify an <see cref="ApplicationPartFactory"/> using <see cref="ProvideApplicationPartFactoryAttribute"/>.
/// Otherwise, <see cref="DefaultApplicationPartFactory"/> is used.
/// </para>
/// </summary>
/// <param name="assembly">The <see cref="Assembly"/>.</param>
/// <returns>An instance of <see cref="ApplicationPartFactory"/>.</returns>
public static ApplicationPartFactory GetApplicationPartFactory(Assembly assembly)
{
if (assembly == null)
{
throw new ArgumentNullException(nameof(assembly));
}
var provideAttribute = assembly.GetCustomAttribute<ProvideApplicationPartFactoryAttribute>();
if (provideAttribute == null)
{
return DefaultApplicationPartFactory.Instance;
}
var type = provideAttribute.GetFactoryType();
if (!typeof(ApplicationPartFactory).IsAssignableFrom(type))
{
throw new InvalidOperationException(Resources.FormatApplicationPartFactory_InvalidFactoryType(
type,
nameof(ProvideApplicationPartFactoryAttribute),
typeof(ApplicationPartFactory)));
}
return (ApplicationPartFactory)Activator.CreateInstance(type);
}
}
}