/
vshmidt
/
masstransit
Обзор
Документация
Войти
/
vshmidt
/
masstransit
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/MassTransit.Abstractions/Internals/Reflection/ReadOnlyPropertyCache.cs
58 строк
2 KB
Chris Patterson
Fixed #3344 - added internal code analysis types for NETSTANDARD2.0 and NET462 builds to allow proper nullable output types for analyzer
06 мар 2023, 21:47
06 мар 2023, 21:47
715a0a2
Код
Авторство
О чём код?
namespace MassTransit.Internals { using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Linq.Expressions; public class ReadOnlyPropertyCache<T> : IReadOnlyPropertyCache<T> { readonly IDictionary<string, ReadOnlyProperty<T>> _properties; public ReadOnlyPropertyCache() { _properties = CreatePropertyCache(); } public IEnumerator<ReadOnlyProperty<T>> GetEnumerator() { return _properties.Values.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public bool TryGetValue(string key, [NotNullWhen(true)] out ReadOnlyProperty<T>? value) { try { return _properties.TryGetValue(key, out value); } catch (KeyNotFoundException) { throw new KeyNotFoundException($"The read only property {key} was not found."); } } static IDictionary<string, ReadOnlyProperty<T>> CreatePropertyCache() { return new Dictionary<string, ReadOnlyProperty<T>>(typeof(T).GetAllProperties() .Where(x => x.CanRead) .GroupBy(x => x.Name, StringComparer.OrdinalIgnoreCase) .Select(x => x.Last()) .Select(x => new ReadOnlyProperty<T>(x)) .ToDictionary(x => x.Property.Name)); } public object Get(Expression<Func<T, object>> propertyExpression, T instance) { return _properties[propertyExpression.GetMemberName()].Get(instance); } } }