diff --git a/TcpDotNet/ClientNode.cs b/TcpDotNet/ClientNode.cs index 28ebda8..bb29390 100644 --- a/TcpDotNet/ClientNode.cs +++ b/TcpDotNet/ClientNode.cs @@ -140,10 +140,7 @@ public abstract class ClientNode : Node return null; } - const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; - ConstructorInfo? constructor = - packetType.GetConstructors(bindingFlags).FirstOrDefault(c => c.GetParameters().Length == 0); - + ConstructorInfo? constructor = GetConstructor(packetType); if (constructor is null) return null; @@ -259,6 +256,24 @@ public abstract class ClientNode : Node return WaitForPacketAsync(completionSource, cancellationToken); } + private static ConstructorInfo? GetConstructor(Type packetType) + { + const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; + ConstructorInfo[] constructors = packetType.GetConstructors(bindingFlags); + ConstructorInfo? constructor = null; + for (var index = 0; index < constructors.Length; index++) + { + ConstructorInfo current = constructors[index]; + if (current.GetParameters().Length == 0) + { + constructor = current; + break; + } + } + + return constructor; + } + private async Task WaitForPacketAsync(TaskCompletionSource completionSource, CancellationToken cancellationToken = default) where TPacket : Packet