using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; namespace Avalonia.Remote.Protocol { public class DefaultMessageTypeResolver : IMessageTypeResolver { private readonly Dictionary _guidsToTypes = new Dictionary(); private readonly Dictionary _typesToGuids = new Dictionary(); [UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "If type was trimmed, we don't need to resolve it in the remove protocol")] public DefaultMessageTypeResolver(params Assembly[] assemblies) { foreach (var asm in (assemblies ?? Array.Empty()).Concat(new[] {typeof(AvaloniaRemoteMessageGuidAttribute).GetTypeInfo().Assembly})) { foreach (var t in asm.ExportedTypes) { var attr = t.GetTypeInfo().GetCustomAttribute(); if (attr != null) { _guidsToTypes[attr.Guid] = t; _typesToGuids[t] = attr.Guid; } } } } public Type GetByGuid(Guid id) => _guidsToTypes[id]; public Guid GetGuid(Type type) => _typesToGuids[type]; } }