Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/aspnet/MessagePack-CSharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/ResolverUtilities.cs')
-rw-r--r--src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/ResolverUtilities.cs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/ResolverUtilities.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/ResolverUtilities.cs
new file mode 100644
index 00000000..a6622181
--- /dev/null
+++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/ResolverUtilities.cs
@@ -0,0 +1,45 @@
+// Copyright (c) All contributors. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+
+using System;
+using System.Reflection;
+using MessagePack.Formatters;
+
+namespace MessagePack.Internal
+{
+ internal static class ResolverUtilities
+ {
+ internal static IMessagePackFormatter ActivateFormatter(Type formatterType, object[] args = null)
+ {
+ if (args == null || args.Length == 0)
+ {
+ if (formatterType.GetConstructor(Type.EmptyTypes) is ConstructorInfo ctor)
+ {
+ return (IMessagePackFormatter)ctor.Invoke(Array.Empty<object>());
+ }
+ else if (FetchSingletonField(formatterType) is FieldInfo instance)
+ {
+ return (IMessagePackFormatter)instance.GetValue(null);
+ }
+ else
+ {
+ throw new MessagePackSerializationException($"The {formatterType.FullName} formatter has no default constructor nor implements the singleton pattern.");
+ }
+ }
+ else
+ {
+ return (IMessagePackFormatter)Activator.CreateInstance(formatterType, args);
+ }
+ }
+
+ internal static FieldInfo FetchSingletonField(Type formatterType)
+ {
+ if (formatterType.GetField("Instance", BindingFlags.Static | BindingFlags.Public) is FieldInfo fieldInfo && fieldInfo.IsInitOnly)
+ {
+ return fieldInfo;
+ }
+
+ return null;
+ }
+ }
+}