/
vshmidt
/
masstransit
Обзор
Документация
Войти
/
vshmidt
/
masstransit
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/MassTransit.Abstractions/Internals/Reflection/ReadWritePropertyCache.cs
76 строк
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 ReadWritePropertyCache<T> : IReadWritePropertyCache<T> { readonly IDictionary<string, ReadWriteProperty<T>> _properties; public ReadWritePropertyCache() { _properties = CreatePropertyCache(false); } public ReadWritePropertyCache(bool includeNonPublic) { _properties = CreatePropertyCache(includeNonPublic); } public ReadWriteProperty<T> this[string name] => _properties[name]; public IEnumerator<ReadWriteProperty<T>> GetEnumerator() { return _properties.Values.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public bool TryGetValue(string key, [NotNullWhen(true)] out ReadWriteProperty<T>? value) { try { return _properties.TryGetValue(key, out value); } catch (KeyNotFoundException) { throw new KeyNotFoundException($"The read only property {key} was not found."); } } public bool TryGetProperty(string propertyName, [NotNullWhen(true)] out ReadWriteProperty<T>? property) { return _properties.TryGetValue(propertyName, out property); } static IDictionary<string, ReadWriteProperty<T>> CreatePropertyCache(bool includeNonPublic) { return new Dictionary<string, ReadWriteProperty<T>>(typeof(T).GetAllProperties() .Where(x => x.CanRead && (includeNonPublic || x.CanWrite)) .Where(x => x.SetMethod != null) .GroupBy(x => x.Name, StringComparer.OrdinalIgnoreCase) .Select(x => x.Last()) .Select(x => new ReadWriteProperty<T>(x)) .ToDictionary(x => x.Property.Name)); } public void Set(Expression<Func<T, object>> propertyExpression, T instance, object value) { _properties[propertyExpression.GetMemberName()].Set(instance, value); } public object Get(Expression<Func<T, object>> propertyExpression, T instance) { return _properties[propertyExpression.GetMemberName()].Get(instance); } } }