1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:45:42 +00:00

Add EndPointExtensions

GetHost() returns the hostname of the endpoint which will either be the
value of DnsEndPoint.Host or IPEndPoint.Address

GetPort() returns the port of the endpoint.
This commit is contained in:
Oliver Booth 2019-11-16 02:04:47 +00:00
parent d037b7f948
commit 90e4552415
No known key found for this signature in database
GPG Key ID: 4B0992B2602C3778
2 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,42 @@
namespace X10D
{
#region Using Directives
using System;
using System.Net;
#endregion
/// <summary>
/// A set of extension methods for <see cref="EndPoint"/> and derived types.
/// </summary>
public static class EndPointExtensions
{
/// <summary>
/// Gets the endpoint hostname.
/// </summary>
/// <param name="endPoint">The endpoint whose hostname to get.</param>
/// <returns>Returns a <see cref="String"/> representing the hostname, which may be an IP or a DNS, or empty
/// string on failure.</returns>
public static string GetHost(this EndPoint endPoint) =>
endPoint switch
{
IPEndPoint ip => ip.Address.ToString(),
DnsEndPoint dns => dns.Host,
_ => String.Empty
};
/// <summary>
/// Gets the endpoint port.
/// </summary>
/// <param name="endPoint">The endpoint whose port to get.</param>
/// <returns>Returns an <see cref="Int32"/> representing the port, or 0 on failure.</returns>
public static int GetPort(this EndPoint endPoint) =>
endPoint switch
{
IPEndPoint ip => ip.Port,
DnsEndPoint dns => dns.Port,
_ => 0
};
}
}

View File

@ -50,6 +50,7 @@
<Compile Include="ConvertibleExtensions.cs" /> <Compile Include="ConvertibleExtensions.cs" />
<Compile Include="DateTimeExtensions.cs" /> <Compile Include="DateTimeExtensions.cs" />
<Compile Include="ComparableExtensions.cs" /> <Compile Include="ComparableExtensions.cs" />
<Compile Include="EndPointExtensions.cs" />
<Compile Include="SingleExtensions.cs" /> <Compile Include="SingleExtensions.cs" />
<Compile Include="DoubleExtensions.cs" /> <Compile Include="DoubleExtensions.cs" />
<Compile Include="Int16Extensions.cs" /> <Compile Include="Int16Extensions.cs" />