/
LicenseProviderAttribute.cs
114 lines (102 loc) · 4.21 KB
/
LicenseProviderAttribute.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
namespace System.ComponentModel
{
/// <summary>
/// Specifies the <see cref='System.ComponentModel.LicenseProvider'/>
/// to use with a class.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class LicenseProviderAttribute : Attribute
{
/// <summary>
/// Specifies the default value, which is no provider. This <see langword='static '/>field is read-only.
/// </summary>
public static readonly LicenseProviderAttribute Default = new LicenseProviderAttribute();
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
private Type? _licenseProviderType;
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
private readonly string? _licenseProviderName;
/// <summary>
/// Initializes a new instance of the <see cref='System.ComponentModel.LicenseProviderAttribute'/> class without a license
/// provider.
/// </summary>
public LicenseProviderAttribute() : this((string?)null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref='System.ComponentModel.LicenseProviderAttribute'/> class with
/// the specified type.
/// </summary>
public LicenseProviderAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] string? typeName)
{
_licenseProviderName = typeName;
}
/// <summary>
/// Initializes a new instance of the <see cref='System.ComponentModel.LicenseProviderAttribute'/> class with
/// the specified type of license provider.
/// </summary>
public LicenseProviderAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type type)
{
_licenseProviderType = type;
}
/// <summary>
/// Gets the license provider to use with the associated class.
/// </summary>
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
public Type? LicenseProvider
{
get
{
if (_licenseProviderType == null && _licenseProviderName != null)
{
_licenseProviderType = Type.GetType(_licenseProviderName);
}
return _licenseProviderType;
}
}
/// <summary>
/// This defines a unique ID for this attribute type. It is used
/// by filtering algorithms to identify two attributes that are
/// the same type. For most attributes, this just returns the
/// Type instance for the attribute. LicenseProviderAttribute overrides this to include the type name and the
/// provider type name.
/// </summary>
public override object TypeId
{
get
{
string? typeName = _licenseProviderName;
if (typeName == null && _licenseProviderType != null)
{
typeName = _licenseProviderType.FullName;
}
return GetType().FullName + typeName;
}
}
public override bool Equals([NotNullWhen(true)] object? value)
{
if (value is LicenseProviderAttribute && value != null)
{
Type? type = ((LicenseProviderAttribute)value).LicenseProvider;
if (type == LicenseProvider)
{
return true;
}
else
{
if (type != null && type.Equals(LicenseProvider))
{
return true;
}
}
}
return false;
}
/// <summary>
/// Returns the hashcode for this object.
/// </summary>
public override int GetHashCode() => base.GetHashCode();
}
}