diff options
author | Andrew Arnott <andrewarnott@live.com> | 2021-08-22 03:03:06 +0300 |
---|---|---|
committer | Andrew Arnott <andrewarnott@live.com> | 2021-08-22 06:12:55 +0300 |
commit | 49fa946120dce638fa0683e0f99f03e1a62a0f2a (patch) | |
tree | 83e3e9e2c1dc9727c43c3739389038590d3299bd | |
parent | 013e3c32a6c5c89e384e36e4a2100e61e72d31d4 (diff) |
Allows client code to avoid tripping over StandardResolver
In AOT-only environments it seems it was impossible to avoid loading `StandardResolver` because the `MessagePackSerializer` had a static property with an initializer that loaded the `StandardResolver`.
After this change, if the `DefaultOptions` getter is never called, or its setter is called first, then the `StandardResolver` can be avoided.
Fixes #832
-rw-r--r-- | src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs | 16 | ||||
-rw-r--r-- | src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs | 2 |
2 files changed, 16 insertions, 2 deletions
diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs index 0e0188cf..93acd668 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs @@ -20,6 +20,7 @@ namespace MessagePack { private const int LZ4NotCompressionSizeInLz4BlockType = 64; private const int MaxHintSize = 1024 * 1024; + private static MessagePackSerializerOptions defaultOptions; /// <summary> /// Gets or sets the default set of options to use when not explicitly specified for a method call. @@ -33,7 +34,20 @@ namespace MessagePack /// If you are an app author, realize that setting this property impacts the entire application so it should only be /// set once, and before any use of <see cref="MessagePackSerializer"/> occurs. /// </remarks> - public static MessagePackSerializerOptions DefaultOptions { get; set; } = MessagePackSerializerOptions.Standard; + public static MessagePackSerializerOptions DefaultOptions + { + get + { + if (defaultOptions is null) + { + defaultOptions = MessagePackSerializerOptions.Standard; + } + + return defaultOptions; + } + + set => defaultOptions = value; + } /// <summary> /// A thread-local, recyclable array that may be used for short bursts of code. diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs index ebf1ca43..211874c4 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs @@ -38,7 +38,7 @@ namespace MessagePack /// <summary> /// Initializes a new instance of the <see cref="MessagePackSerializerOptions"/> class. /// </summary> - protected internal MessagePackSerializerOptions(IFormatterResolver resolver) + public MessagePackSerializerOptions(IFormatterResolver resolver) { this.Resolver = resolver ?? throw new ArgumentNullException(nameof(resolver)); } |