Skip to content

Commit

Permalink
Improved AOT compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Nov 29, 2024
1 parent f9c53d5 commit 0d72f21
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
7 changes: 6 additions & 1 deletion MimeKit/Cryptography/CryptographyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System.Reflection;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace MimeKit.Cryptography {
/// <summary>
Expand Down Expand Up @@ -768,7 +769,11 @@ public static CryptographyContext Create (string protocol)
/// <para>-or-</para>
/// <para><paramref name="type"/> does not have a parameterless constructor.</para>
/// </exception>
public static void Register (Type type)
public static void Register (
#if NET8_0_OR_GREATER
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
#endif
Type type)
{
if (type == null)
throw new ArgumentNullException (nameof (type));
Expand Down
2 changes: 1 addition & 1 deletion MimeKit/Cryptography/SecureMimeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public abstract class SecureMimeContext : CryptographyContext, ISecureMimeContex
static readonly string[] ProtocolSubtypes = { "pkcs7-signature", "pkcs7-mime", "pkcs7-keys", "x-pkcs7-signature", "x-pkcs7-mime", "x-pkcs7-keys" };
internal const X509KeyUsageFlags DigitalSignatureKeyUsageFlags = X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.NonRepudiation;
#if NET8_0_OR_GREATER
internal static readonly int EncryptionAlgorithmCount = Enum.GetValues<EncryptionAlgorithm> ().Length;
internal static readonly int EncryptionAlgorithmCount = Enum.GetValuesAsUnderlyingType<EncryptionAlgorithm> ().Length;
#else
internal static readonly int EncryptionAlgorithmCount = Enum.GetValues (typeof (EncryptionAlgorithm)).Length;
#endif
Expand Down
11 changes: 7 additions & 4 deletions MimeKit/HeaderId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -854,15 +854,18 @@ public static class HeaderIdExtensions
static HeaderIdExtensions ()
{
#if NET8_0_OR_GREATER
var values = Enum.GetValues<HeaderId> ();
var values = Enum.GetValuesAsUnderlyingType<HeaderId> ();
#else
var values = (HeaderId[]) Enum.GetValues (typeof (HeaderId));
var values = Enum.GetValues (typeof (HeaderId));
#endif

IdMapping = new Dictionary<string, HeaderId> (values.Length - 1, MimeUtils.OrdinalIgnoreCase);

for (int i = 0; i < values.Length - 1; i++)
IdMapping.Add (HeaderNames[i], values[i]);
for (int i = 0; i < values.Length - 1; i++) {
var value = (HeaderId) values.GetValue (i);

IdMapping.Add (HeaderNames[i], value);
}
}

/// <summary>
Expand Down
8 changes: 5 additions & 3 deletions MimeKit/MessagePartial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,14 @@ public static IEnumerable<MimeMessage> Split (MimeMessage message, int maxSize)
var options = FormatOptions.Default.Clone ();

#if NET8_0_OR_GREATER
var ids = Enum.GetValues<HeaderId> ();
var ids = Enum.GetValuesAsUnderlyingType<HeaderId> ();
#else
var ids = (HeaderId[]) Enum.GetValues (typeof (HeaderId));
var ids = Enum.GetValues (typeof (HeaderId));
#endif

foreach (var id in ids) {
for (int i = 0; i < ids.Length; i++) {
var id = (HeaderId) ids.GetValue (i);

switch (id) {
case HeaderId.Subject:
case HeaderId.MessageId:
Expand Down
2 changes: 1 addition & 1 deletion MimeKit/Text/HtmlAttributeId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ static HtmlAttributeIdExtensions ()
for (int i = 1; i < values.Length; i++) {
var value = (HtmlAttributeId) values.GetValue (i);

IdMapping.Add (value.ToAttributeName (), value);
IdMapping.Add (AttributeNames[i - 1], value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion MimeKit/Text/HtmlTagId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ static HtmlTagIdExtensions ()
for (int i = 1; i < values.Length; i++) {
var value = (HtmlTagId) values.GetValue (i);

IdMapping.Add (value.ToHtmlTagName (), value);
IdMapping.Add (TagNames[i - 1], value);
}
}

Expand Down

0 comments on commit 0d72f21

Please sign in to comment.