|
| 1 | +// * |
| 2 | +// ***************************************************************************** |
| 3 | +// * |
| 4 | +// * Copyright (c) 2025 Deskasoft International |
| 5 | +// * |
| 6 | +// * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// * of this software and associated documentation files (the ""Software""), to deal |
| 8 | +// * in the Software without restriction, including without limitation the rights |
| 9 | +// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// * copies of the Software, and to permit persons to whom the Software is |
| 11 | +// * furnished to do so, subject to the following conditions: |
| 12 | +// * |
| 13 | +// * The above copyright notice and this permission notice shall be included in all |
| 14 | +// * copies or substantial portions of the Software. |
| 15 | +// * |
| 16 | +// * THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// * SOFTWARE. |
| 23 | +// * |
| 24 | +// * |
| 25 | +// * Please refer to LICENSE file. |
| 26 | +// * |
| 27 | +// ****************************************************************************** |
| 28 | +// * |
| 29 | + |
| 30 | +using HashifyNet.Core.Utilities; |
| 31 | +using System; |
| 32 | + |
| 33 | +namespace HashifyNet.Core |
| 34 | +{ |
| 35 | + /// <summary> |
| 36 | + /// Specifies that a class implements a hash algorithm. |
| 37 | + /// </summary> |
| 38 | + /// <remarks>This attribute is used to annotate classes that provide an implementation of a hash algorithm. It |
| 39 | + /// is intended for internal use and is not inherited by derived classes.</remarks> |
| 40 | + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] |
| 41 | + internal class HashAlgorithmImplementationAttribute : Attribute |
| 42 | + { |
| 43 | + /// <summary> |
| 44 | + /// Gets the interface type that the current class implements. |
| 45 | + /// </summary> |
| 46 | + /// <remarks>This property is useful for scenarios where reflection or type analysis is required to determine |
| 47 | + /// the specific interface implemented by the class.</remarks> |
| 48 | + public Type ImplementedInterface { get; } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets the concrete configuration type associated with the current instance. |
| 52 | + /// </summary> |
| 53 | + public Type ConcreteConfig { get; } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Initializes a new instance of the <see cref="HashAlgorithmImplementationAttribute"/> class, specifying the |
| 57 | + /// interface implemented by the hash algorithm and the concrete configuration type. |
| 58 | + /// </summary> |
| 59 | + /// <param name="implementedInterface">The interface type that the hash algorithm implementation adheres to. This parameter must represent an interface |
| 60 | + /// type.</param> |
| 61 | + /// <param name="concreteConfig">The concrete class type that provides the configuration for the hash algorithm implementation. This parameter |
| 62 | + /// must represent a class type.</param> |
| 63 | + /// <exception cref="ArgumentNullException">Thrown if <paramref name="implementedInterface"/> or <paramref name="concreteConfig"/> is <see langword="null"/>.</exception> |
| 64 | + /// <exception cref="ArgumentException">Thrown if <paramref name="implementedInterface"/> is not an interface type, or if <paramref |
| 65 | + /// name="concreteConfig"/> is not a class type.</exception> |
| 66 | + public HashAlgorithmImplementationAttribute(Type implementedInterface, Type concreteConfig) |
| 67 | + { |
| 68 | + if (implementedInterface == null) |
| 69 | + { |
| 70 | + throw new ArgumentNullException(nameof(implementedInterface), $"{nameof(implementedInterface)} must not be null."); |
| 71 | + } |
| 72 | + |
| 73 | + string memberName = implementedInterface.FullName; |
| 74 | + |
| 75 | + if (!implementedInterface.IsInterface) |
| 76 | + { |
| 77 | + throw new ArgumentException($"{memberName}: The provided type must be an interface.", nameof(implementedInterface)); |
| 78 | + } |
| 79 | + |
| 80 | + if (concreteConfig == null) |
| 81 | + { |
| 82 | + throw new ArgumentNullException(nameof(concreteConfig), $"{memberName}: {nameof(concreteConfig)} must not be null."); |
| 83 | + } |
| 84 | + |
| 85 | + if (!concreteConfig.IsClass) |
| 86 | + { |
| 87 | + throw new ArgumentException($"{memberName}: The provided type for concrete config must be a class.", nameof(concreteConfig)); |
| 88 | + } |
| 89 | + |
| 90 | + if (!implementedInterface.HasInterface(typeof(IHashFunction<>))) |
| 91 | + { |
| 92 | + throw new ArgumentException($"{memberName}: The provided interface type must implement IHashFunction<>.", nameof(implementedInterface)); |
| 93 | + } |
| 94 | + |
| 95 | + if (!concreteConfig.HasInterface(typeof(IHashConfig<>))) |
| 96 | + { |
| 97 | + throw new ArgumentException($"{memberName}: The provided concrete config type must implement IHashConfig<>.", nameof(concreteConfig)); |
| 98 | + } |
| 99 | + |
| 100 | + ImplementedInterface = implementedInterface; |
| 101 | + ConcreteConfig = concreteConfig; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments