-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added abstractions for System.Net.NetworkInformation.
- Loading branch information
Showing
59 changed files
with
2,173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/Thinktecture.Abstractions/Collections/Generic/CollectionAbstractionAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
|
||
namespace Thinktecture.Collections.Generic | ||
{ | ||
/// <summary> | ||
/// Adapter for collections. | ||
/// </summary> | ||
/// <typeparam name="TAbstractionItem">Item type of the abstraction.</typeparam> | ||
/// <typeparam name="TImplementationItem">Item type of the implementation.</typeparam> | ||
/// <typeparam name="TImplementation">Type of the implementation.</typeparam> | ||
public class CollectionAbstractionAdapter<TAbstractionItem, TImplementationItem, TImplementation> : AbstractionAdapter, ICollectionAbstraction<TAbstractionItem, TImplementationItem, TImplementation> | ||
where TAbstractionItem : IAbstraction<TImplementationItem> | ||
where TImplementation : ICollection<TImplementationItem> | ||
{ | ||
/// <summary> | ||
/// Inner collection. | ||
/// </summary> | ||
protected readonly TImplementation Collection; | ||
|
||
private readonly Func<TImplementationItem, TAbstractionItem> _toInterface; | ||
|
||
/// <inheritdoc /> | ||
public int Count => Collection.Count; | ||
|
||
/// <inheritdoc /> | ||
public bool IsReadOnly => Collection.IsReadOnly; | ||
|
||
/// <summary> | ||
/// Initializes new instance of <see cref="CollectionAbstractionAdapter{TAbstractionItem,TImplementationItem,TImplementation}"/>. | ||
/// </summary> | ||
/// <param name="collection">Collection to be used by the adapter.</param> | ||
/// <param name="toInterface">Converts an item of <typeparamref name="TImplementation"/> to type <typeparamref name="TAbstractionItem"/>.</param> | ||
public CollectionAbstractionAdapter(TImplementation collection, Func<TImplementationItem, TAbstractionItem> toInterface) | ||
: base(collection) | ||
{ | ||
if (collection == null) | ||
throw new ArgumentNullException(nameof(collection)); | ||
if (toInterface == null) | ||
throw new ArgumentNullException(nameof(toInterface)); | ||
Collection = collection; | ||
_toInterface = toInterface; | ||
} | ||
|
||
/// <inheritdoc /> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public new TImplementation UnsafeConvert() | ||
{ | ||
return Collection; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IEnumerator<TAbstractionItem> GetEnumerator() | ||
{ | ||
foreach (var item in Collection) | ||
{ | ||
yield return _toInterface(item); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Add(TAbstractionItem item) | ||
{ | ||
Collection.Add(item.ToImplementation()); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Add(TImplementationItem item) | ||
{ | ||
Collection.Add(item); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Clear() | ||
{ | ||
Collection.Clear(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool Contains(TAbstractionItem item) | ||
{ | ||
return Collection.Contains(item.ToImplementation()); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool Contains(TImplementationItem item) | ||
{ | ||
return Collection.Contains(item); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void CopyTo(TAbstractionItem[] array, int arrayIndex) | ||
{ | ||
Collection.CopyTo(array.ToImplementation<TAbstractionItem, TImplementationItem>(), arrayIndex); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void CopyTo(TImplementationItem[] array, int arrayIndex) | ||
{ | ||
Collection.CopyTo(array, arrayIndex); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool Remove(TAbstractionItem item) | ||
{ | ||
return Collection.Remove(item.ToImplementation()); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool Remove(TImplementationItem item) | ||
{ | ||
return Collection.Remove(item); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Thinktecture.Abstractions/Collections/Generic/ICollectionAbstraction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
|
||
namespace Thinktecture.Collections.Generic | ||
{ | ||
/// <summary> | ||
/// An abstraction for collections. | ||
/// </summary> | ||
/// <typeparam name="TAbstractionItem">Type of the abstraction.</typeparam> | ||
/// <typeparam name="TImplementationItem">Tzpe of the item of the implementation.</typeparam> | ||
/// <typeparam name="TImplementation">Type of the implementation.</typeparam> | ||
public interface ICollectionAbstraction<TAbstractionItem, in TImplementationItem, out TImplementation> : IAbstraction<TImplementation>, ICollection<TAbstractionItem> | ||
where TAbstractionItem : IAbstraction<TImplementationItem> | ||
where TImplementation : ICollection<TImplementationItem> | ||
{ | ||
/// <summary> | ||
/// Gets inner instance of <typeparamref name="TImplementation"/>. | ||
/// It is not intended to be used directly. Use <see cref="CollectionExtensions.ToImplementation{TAbstractionItem,TImplementationItem,TImplementation}"/> instead. | ||
/// </summary> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
new TImplementation UnsafeConvert(); | ||
|
||
/// <summary> | ||
/// Adds item to collection. | ||
/// </summary> | ||
/// <param name="item">Item to add.</param> | ||
void Add(TImplementationItem item); | ||
|
||
/// <summary> | ||
/// Gets an indicator whether the item is in collection or not. | ||
/// </summary> | ||
/// <param name="item">Item to check for.</param> | ||
/// <returns><c>true</c> if the item is in collection; <c>false</c> otherwise.</returns> | ||
bool Contains(TImplementationItem item); | ||
|
||
/// <summary> | ||
/// Copies the collection to provided array. | ||
/// </summary> | ||
/// <param name="array">Array to copy into.</param> | ||
/// <param name="arrayIndex">Index to start to insert from.</param> | ||
void CopyTo(TImplementationItem[] array, int arrayIndex); | ||
|
||
/// <summary> | ||
/// Removes the item from collection. | ||
/// </summary> | ||
/// <param name="item">Item to remove.</param> | ||
/// <returns><c>true</c> if the item has been removed; <c>false</c> otherwise.</returns> | ||
bool Remove(TImplementationItem item); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Thinktecture.Abstractions/Extensions/AbstractionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace Thinktecture | ||
{ | ||
/// <summary> | ||
/// Extensions for <see cref="IAbstraction"/>. | ||
/// </summary> | ||
public static class AbstractionExtensions | ||
{ | ||
/// <summary> | ||
/// Converts provided abstraction to implementation. | ||
/// </summary> | ||
/// <param name="abstraction">Abstraction to convert.</param> | ||
/// <returns>Converted abstraction.</returns> | ||
public static TImplementation ToImplementation<TImplementation>(this IAbstraction<TImplementation> abstraction) | ||
{ | ||
return abstraction != null ? abstraction.UnsafeConvert() : default(TImplementation); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Thinktecture.Abstractions/Extensions/CollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Collections.Generic; | ||
using Thinktecture.Collections.Generic; | ||
|
||
namespace Thinktecture | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="ICollection{T}"/> | ||
/// </summary> | ||
public static class CollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Converts a collection of type <typeparamref name="TAbstractionItem"/> to <typeparamref name="TImplementation"/>. | ||
/// </summary> | ||
/// <param name="collection">Collection to convert</param> | ||
/// <typeparam name="TAbstractionItem">Type of the abstraction.</typeparam> | ||
/// <typeparam name="TImplementationItem">Type of the item of the implementation.</typeparam> | ||
/// <typeparam name="TImplementation">Type of the implementation.</typeparam> | ||
/// <returns>Converted colletion.</returns> | ||
public static TImplementation ToImplementation<TAbstractionItem, TImplementationItem, TImplementation>(this ICollectionAbstraction<TAbstractionItem, TImplementationItem, TImplementation> collection) | ||
where TAbstractionItem : IAbstraction<TImplementationItem> | ||
where TImplementation : ICollection<TImplementationItem> | ||
{ | ||
return collection == null ? default(TImplementation) : collection.UnsafeConvert(); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/Thinktecture.Abstractions/Thinktecture.Abstractions.xproj.DotSettings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=extensions/@EntryIndexedValue">True</s:Boolean> | ||
</wpf:ResourceDictionary> |
35 changes: 35 additions & 0 deletions
35
src/Thinktecture.Net.NetworkInformation.Abstractions/Extensions/ArrayExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#if NETSTANDARD1_3 || NET45 || NET46 | ||
|
||
using System.Net.NetworkInformation; | ||
using Thinktecture.Net.NetworkInformation; | ||
|
||
namespace Thinktecture | ||
{ | ||
/// <summary> | ||
/// Extensions for arrays. | ||
/// </summary> | ||
public static class ArrayExtensions | ||
{ | ||
/// <summary> | ||
/// Converts an array of <see cref="NetworkInterface"/> to an array of <see cref="INetworkInterface"/>. | ||
/// </summary> | ||
/// <param name="elements">Array to convert.</param> | ||
/// <returns>An array of <see cref="INetworkInterface"/>.</returns> | ||
public static INetworkInterface[] ToInterface(this NetworkInterface[] elements) | ||
{ | ||
if (elements == null) | ||
return null; | ||
|
||
var abstractions = new INetworkInterface[elements.Length]; | ||
|
||
for (var i = 0; i < elements.Length; i++) | ||
{ | ||
abstractions[i] = elements[i].ToInterface(); | ||
} | ||
|
||
return abstractions; | ||
} | ||
} | ||
} | ||
|
||
#endif |
27 changes: 27 additions & 0 deletions
27
...orkInformation.Abstractions/Extensions/GatewayIPAddressInformationCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#if NETSTANDARD1_3 || NET45 || NET46 | ||
|
||
using System.Net.NetworkInformation; | ||
using Thinktecture.Net.NetworkInformation; | ||
using Thinktecture.Net.NetworkInformation.Adapters; | ||
|
||
namespace Thinktecture | ||
{ | ||
/// <summary> | ||
/// Extensions for <see cref="GatewayIPAddressInformationCollection"/>. | ||
/// </summary> | ||
// ReSharper disable once InconsistentNaming | ||
public static class GatewayIPAddressInformationCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Converts provided collection to <see cref="IGatewayIPAddressInformationCollection"/>. | ||
/// </summary> | ||
/// <param name="collection">Collection to convert.</param> | ||
/// <returns>Converted collection.</returns> | ||
public static IGatewayIPAddressInformationCollection ToInterface(this GatewayIPAddressInformationCollection collection) | ||
{ | ||
return (collection == null) ? null : new GatewayIPAddressInformationCollectionAdapter(collection); | ||
} | ||
} | ||
} | ||
|
||
#endif |
27 changes: 27 additions & 0 deletions
27
...e.Net.NetworkInformation.Abstractions/Extensions/GatewayIPAddressInformationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#if NETSTANDARD1_3 || NET45 || NET46 | ||
|
||
using System.Net.NetworkInformation; | ||
using Thinktecture.Net.NetworkInformation; | ||
using Thinktecture.Net.NetworkInformation.Adapters; | ||
|
||
namespace Thinktecture | ||
{ | ||
/// <summary> | ||
/// Extensions for <see cref="GatewayIPAddressInformation"/>. | ||
/// </summary> | ||
// ReSharper disable once InconsistentNaming | ||
public static class GatewayIPAddressInformationExtensions | ||
{ | ||
/// <summary> | ||
/// Converts provided info to <see cref="IGatewayIPAddressInformation"/>. | ||
/// </summary> | ||
/// <param name="info">Info to convert.</param> | ||
/// <returns>Converted info.</returns> | ||
public static IGatewayIPAddressInformation ToInterface(this GatewayIPAddressInformation info) | ||
{ | ||
return (info == null) ? null : new GatewayIPAddressInformationAdapter(info); | ||
} | ||
} | ||
} | ||
|
||
#endif |
27 changes: 27 additions & 0 deletions
27
...et.NetworkInformation.Abstractions/Extensions/IPAddressInformationCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#if NETSTANDARD1_3 || NET45 || NET46 | ||
|
||
using System.Net.NetworkInformation; | ||
using Thinktecture.Net.NetworkInformation; | ||
using Thinktecture.Net.NetworkInformation.Adapters; | ||
|
||
namespace Thinktecture | ||
{ | ||
/// <summary> | ||
/// Extensions for <see cref="IPAddressInformationCollection"/>. | ||
/// </summary> | ||
// ReSharper disable once InconsistentNaming | ||
public static class IPAddressInformationCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Converts provided collection to <see cref="IIPAddressInformationCollection"/>. | ||
/// </summary> | ||
/// <param name="collection">Collection to convert.</param> | ||
/// <returns>Converted collection.</returns> | ||
public static IIPAddressInformationCollection ToInterface(this IPAddressInformationCollection collection) | ||
{ | ||
return (collection == null) ? null : new IPAddressInformationCollectionAdapter(collection); | ||
} | ||
} | ||
} | ||
|
||
#endif |
Oops, something went wrong.