mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-26 06:48:47 +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:
parent
d037b7f948
commit
90e4552415
42
X10D/EndPointExtensions.cs
Normal file
42
X10D/EndPointExtensions.cs
Normal 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
|
||||
};
|
||||
}
|
||||
}
|
@ -50,6 +50,7 @@
|
||||
<Compile Include="ConvertibleExtensions.cs" />
|
||||
<Compile Include="DateTimeExtensions.cs" />
|
||||
<Compile Include="ComparableExtensions.cs" />
|
||||
<Compile Include="EndPointExtensions.cs" />
|
||||
<Compile Include="SingleExtensions.cs" />
|
||||
<Compile Include="DoubleExtensions.cs" />
|
||||
<Compile Include="Int16Extensions.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user