-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
ApplicationPartFactory.cs
56 lines (49 loc) · 2.36 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
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)
{
ArgumentNullException.ThrowIfNull(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)!;
}
}