using System; using System.Reflection; using UnityEngine; namespace DanroJump.Hardware.Com { public sealed class ReflectionComPortProvider : IComPortProvider { private static readonly Type SerialPortType = Type.GetType("System.IO.Ports.SerialPort, System"); public string[] GetPortNames() { if (SerialPortType == null) { Debug.LogWarning("[ComSystem] System.IO.Ports.SerialPort is not available in this Unity profile."); return Array.Empty(); } try { return (string[])SerialPortType.GetMethod("GetPortNames", BindingFlags.Public | BindingFlags.Static) ?.Invoke(null, null) ?? Array.Empty(); } catch (Exception exception) { Debug.LogWarning($"[ComSystem] Failed to list COM ports: {Unwrap(exception).Message}"); return Array.Empty(); } } private static Exception Unwrap(Exception exception) { return exception is TargetInvocationException { InnerException: not null } ? exception.InnerException : exception; } } }