-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MailAddressConverter and MailAddressCollectionConverter
- Loading branch information
Showing
4 changed files
with
94 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
42 changes: 42 additions & 0 deletions
42
src/Louis/ComponentModel/MailAddressCollectionConverter.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,42 @@ | ||
// Copyright (c) Tenacom and contributors. Licensed under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using System.Net.Mail; | ||
using CommunityToolkit.Diagnostics; | ||
|
||
namespace Louis.ComponentModel; | ||
|
||
/// <summary> | ||
/// Provides a type converter to convert <see cref="MailAddressCollection"/> instances to and from strings. | ||
/// </summary> | ||
public sealed class MailAddressCollectionConverter : SimpleStringConverter<MailAddressCollection> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MailAddressCollectionConverter"/> class. | ||
/// </summary> | ||
public MailAddressCollectionConverter() | ||
: base(DoConvertFromString, DoConvertToString) | ||
{ | ||
} | ||
|
||
private static MailAddressCollection DoConvertFromString(ITypeDescriptorContext? context, CultureInfo? culture, string value) | ||
{ | ||
Guard.IsNotEmpty(value, nameof(value)); | ||
var result = new MailAddressCollection(); | ||
try | ||
{ | ||
result.Add(value); | ||
} | ||
catch (FormatException e) | ||
{ | ||
ThrowHelper.ThrowArgumentException(nameof(value), "Value should be a valid e-mail address, or valid e-mail addresses separated by commas.", e); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private static string DoConvertToString(ITypeDescriptorContext? context, CultureInfo? culture, MailAddressCollection value) => value.ToString(); | ||
} |
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,47 @@ | ||
// Copyright (c) Tenacom and contributors. Licensed under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using System.Net.Mail; | ||
using CommunityToolkit.Diagnostics; | ||
|
||
namespace Louis.ComponentModel; | ||
|
||
/// <summary> | ||
/// Provides a type converter to convert <see cref="MailAddress"/> instances to and from strings. | ||
/// </summary> | ||
public sealed class MailAddressConverter : SimpleStringConverter<MailAddress> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MailAddressConverter"/> class. | ||
/// </summary> | ||
public MailAddressConverter() | ||
: base(DoConvertFromString, DoConvertToString) | ||
{ | ||
} | ||
|
||
private static MailAddress DoConvertFromString(ITypeDescriptorContext? context, CultureInfo? culture, string value) | ||
{ | ||
#if NET5_0_OR_GREATER | ||
return MailAddress.TryCreate(value, out var result) ? result : ThrowOnInvalidString(nameof(value)); | ||
#else | ||
try | ||
{ | ||
return new MailAddress(value); | ||
} | ||
catch (FormatException ex) | ||
{ | ||
return ThrowOnInvalidString(nameof(value), ex); | ||
} | ||
#endif | ||
|
||
[DoesNotReturn] | ||
static MailAddress ThrowOnInvalidString(string parameterName, Exception? innerException = null) | ||
=> ThrowHelper.ThrowArgumentException<MailAddress>(parameterName, "Value should be a valid e-mail address.", innerException); | ||
} | ||
|
||
private static string DoConvertToString(ITypeDescriptorContext? context, CultureInfo? culture, MailAddress value) => value.ToString(); | ||
} |
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