-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
ProvideApplicationPartFactoryAttribute.cs
49 lines (42 loc) · 1.76 KB
/
ProvideApplicationPartFactoryAttribute.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.AspNetCore.Mvc.Core;
namespace Microsoft.AspNetCore.Mvc.ApplicationParts;
/// <summary>
/// Provides a <see cref="ApplicationPartFactory"/> type.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
public sealed class ProvideApplicationPartFactoryAttribute : Attribute
{
private readonly Type? _applicationPartFactoryType;
private readonly string? _applicationPartFactoryTypeName;
/// <summary>
/// Creates a new instance of <see cref="ProvideApplicationPartFactoryAttribute"/> with the specified type.
/// </summary>
/// <param name="factoryType">The factory type.</param>
public ProvideApplicationPartFactoryAttribute(Type factoryType)
{
_applicationPartFactoryType = factoryType ?? throw new ArgumentNullException(nameof(factoryType));
}
/// <summary>
/// Creates a new instance of <see cref="ProvideApplicationPartFactoryAttribute"/> with the specified type name.
/// </summary>
/// <param name="factoryTypeName">The assembly qualified type name.</param>
public ProvideApplicationPartFactoryAttribute(string factoryTypeName)
{
if (string.IsNullOrEmpty(factoryTypeName))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(factoryTypeName));
}
_applicationPartFactoryTypeName = factoryTypeName;
}
/// <summary>
/// Gets the factory type.
/// </summary>
/// <returns></returns>
public Type GetFactoryType()
{
return _applicationPartFactoryType ??
Type.GetType(_applicationPartFactoryTypeName!, throwOnError: true)!;
}
}