| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="PropertyMetadata.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Concurrent; |
| | | 9 | | using System.Diagnostics.CodeAnalysis; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Metadata; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Represents metadata associated with a property. This class is used to store and manage metadata for a specific prope |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class PropertyMetadata |
| | | 17 | | { |
| | 378 | 18 | | private readonly ConcurrentDictionary<Type, object> _features = new(); |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Sets the feature for the specified type. This method allows you to associate a specific piece of metadata (refer |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="feature">The feature to set for the specified type.</param> |
| | | 24 | | /// <typeparam name="TFeature">The type of the feature.</typeparam> |
| | | 25 | | public void SetFeature<TFeature>(TFeature feature) |
| | 57 | 26 | | where TFeature : class => _features[typeof(TFeature)] = feature; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Tries to get the feature of the specified type. This method checks if there is a feature associated with the pro |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="feature">When this method returns, contains the feature of the specified type if it exists; otherwi |
| | | 32 | | /// <typeparam name="TFeature">The type of the feature to retrieve.</typeparam> |
| | | 33 | | /// <returns>true if the feature of the specified type exists; otherwise, false.</returns> |
| | | 34 | | public bool TryGetFeature<TFeature>([NotNullWhen(true)] out TFeature? feature) |
| | | 35 | | where TFeature : class |
| | | 36 | | { |
| | 8655 | 37 | | if (_features.TryGetValue(typeof(TFeature), out var value)) |
| | | 38 | | { |
| | 387 | 39 | | feature = (TFeature)value; |
| | 387 | 40 | | return true; |
| | | 41 | | } |
| | | 42 | | |
| | 8268 | 43 | | feature = null; |
| | 8268 | 44 | | return false; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the feature of the specified type, or creates a new instance if it does not exist. This method checks if th |
| | | 49 | | /// </summary> |
| | | 50 | | /// <typeparam name="TFeature">The type of the feature to retrieve or create.</typeparam> |
| | | 51 | | /// <returns>The existing or newly created feature of the specified type.</returns> |
| | | 52 | | public TFeature GetOrCreate<TFeature>() |
| | | 53 | | where TFeature : class, new() |
| | 60 | 54 | | => (TFeature)_features.GetOrAdd(typeof(TFeature), static _ => new TFeature()); |
| | | 55 | | } |
| | | 56 | | |