| | 1 | | using System.IO; |
| | 2 | | using System.Reflection; |
| | 3 | | using System.Text.Json; |
| | 4 | | using System.Xml; |
| | 5 | | using System.Xml.Serialization; |
| | 6 | |
|
| | 7 | | namespace Towel; |
| | 8 | |
|
| | 9 | | /// <summary>Static class containing serialization code.</summary> |
| | 10 | | public static partial class Serialization |
| | 11 | | { |
| | 12 | | #region Shared |
| | 13 | |
|
| 1 | 14 | | internal static readonly XmlWriterSettings DefaultXmlWriterSettings = |
| 1 | 15 | | new() { OmitXmlDeclaration = true, }; |
| | 16 | |
|
| | 17 | | #endregion |
| | 18 | |
|
| | 19 | | #region System.Xml.Serialization.XmlSerializer .NET Wrapper (Default) |
| | 20 | |
|
| | 21 | | #region To XML |
| | 22 | |
|
| | 23 | | /// <summary>Wrapper for the default XML serialization in .NET using XmlSerializer.</summary> |
| | 24 | | /// <typeparam name="T">The type of object to serialize.</typeparam> |
| | 25 | | /// <param name="value">The value to serialize.</param> |
| | 26 | | /// <returns>The XML serialzation of the value.</returns> |
| | 27 | | public static string DefaultToXml<T>(T value) => |
| 0 | 28 | | DefaultToXml(value, DefaultXmlWriterSettings); |
| | 29 | |
|
| | 30 | | /// <summary>Wrapper for the default XML serialization in .NET using XmlSerializer.</summary> |
| | 31 | | /// <typeparam name="T">The type of object to serialize.</typeparam> |
| | 32 | | /// <param name="value">The value to serialize.</param> |
| | 33 | | /// <param name="xmlWriterSettings">The settings of the XML writer during serialization.</param> |
| | 34 | | /// <returns>The XML serialzation of the value.</returns> |
| | 35 | | public static string DefaultToXml<T>(T value, XmlWriterSettings xmlWriterSettings) |
| 0 | 36 | | { |
| 0 | 37 | | using StringWriter stringWriter = new(); |
| 0 | 38 | | DefaultToXml(value, stringWriter, xmlWriterSettings); |
| 0 | 39 | | return stringWriter.ToString(); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary>Wrapper for the default XML serialization in .NET using XmlSerializer.</summary> |
| | 43 | | /// <typeparam name="T">The type of object to serialize.</typeparam> |
| | 44 | | /// <param name="value">The value to serialize.</param> |
| | 45 | | /// <param name="textWriter">The text writer to output the XML serialization to.</param> |
| | 46 | | public static void DefaultToXml<T>(T value, TextWriter textWriter) => |
| 0 | 47 | | DefaultToXml(value, textWriter, DefaultXmlWriterSettings); |
| | 48 | |
|
| | 49 | | /// <summary>Wrapper for the default XML serialization in .NET using XmlSerializer.</summary> |
| | 50 | | /// <typeparam name="T">The type of object to serialize.</typeparam> |
| | 51 | | /// <param name="value">The value to serialize.</param> |
| | 52 | | /// <param name="textWriter">The text writer to output the XML serialization to.</param> |
| | 53 | | /// <param name="xmlWriterSettings">The settings of the XML writer during serialization.</param> |
| | 54 | | public static void DefaultToXml<T>(T value, TextWriter textWriter, XmlWriterSettings xmlWriterSettings) |
| 0 | 55 | | { |
| 0 | 56 | | using XmlWriter xmlWriter = XmlWriter.Create(textWriter, xmlWriterSettings); |
| 0 | 57 | | XmlSerializer serializer = new(typeof(T)); |
| 0 | 58 | | serializer.Serialize(xmlWriter, value); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | #endregion |
| | 62 | |
|
| | 63 | | #region From XML |
| | 64 | |
|
| | 65 | | /// <summary>Wrapper for the default XML deserialization in .NET using XmlSerializer.</summary> |
| | 66 | | /// <typeparam name="T">The type of object to deserialize.</typeparam> |
| | 67 | | /// <param name="string">The string containing the XML content to deserialize.</param> |
| | 68 | | /// <returns>The deserialized value.</returns> |
| | 69 | | public static T? DefaultFromXml<T>(string @string) |
| 0 | 70 | | { |
| 0 | 71 | | using StringReader stringReader = new(@string); |
| 0 | 72 | | return DefaultFromXml<T>(stringReader); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary>Wrapper for the default XML deserialization in .NET using XmlSerializer.</summary> |
| | 76 | | /// <typeparam name="T">The type of object to deserialize.</typeparam> |
| | 77 | | /// <param name="textReader">The text reader providing the XML to deserialize.</param> |
| | 78 | | /// <returns>The deserialized value.</returns> |
| | 79 | | public static T? DefaultFromXml<T>(TextReader textReader) |
| 0 | 80 | | { |
| 0 | 81 | | XmlSerializer serializer = new(typeof(T)); |
| 0 | 82 | | return (T?)serializer.Deserialize(textReader); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | #endregion |
| | 86 | |
|
| | 87 | | #endregion |
| | 88 | |
|
| | 89 | | #region Static Delegates |
| | 90 | |
|
| | 91 | | #region XML |
| | 92 | |
|
| | 93 | | internal static class StaticDelegateConstants |
| | 94 | | { |
| | 95 | | internal const string Name = "Delegate"; |
| | 96 | | internal const string DeclaringType = "Method.DeclaringType.AssemblyQualifiedName"; |
| | 97 | | internal const string MethodName = "Method.Name"; |
| | 98 | | internal const string Parameters = "Method.GetParameters"; |
| | 99 | | internal const string ParameterType = "ParameterType.AssemblyQualifiedName"; |
| | 100 | | internal const string ReturnType = "Method.ReturnType"; |
| | 101 | | } |
| | 102 | |
|
| | 103 | | #region To XML |
| | 104 | |
|
| | 105 | | /// <summary>Serializes a static delegate to XML.</summary> |
| | 106 | | /// <typeparam name="T">The type of delegate to serialize.</typeparam> |
| | 107 | | /// <param name="delegate">The delegate to serialize.</param> |
| | 108 | | /// <returns>The XML serialization of the delegate.</returns> |
| | 109 | | /// <exception cref="NotSupportedException"> |
| | 110 | | /// Thrown when the delegate is pointing to a non-static method. |
| | 111 | | /// </exception> |
| | 112 | | /// <exception cref="NotSupportedException"> |
| | 113 | | /// Thrown when the delegate is pointing to a local function. |
| | 114 | | /// </exception> |
| | 115 | | public static string StaticDelegateToXml<T>(T @delegate) where T : Delegate => |
| 7 | 116 | | StaticDelegateToXml(@delegate, DefaultXmlWriterSettings); |
| | 117 | |
|
| | 118 | | /// <summary>Serializes a static delegate to XML.</summary> |
| | 119 | | /// <typeparam name="T">The type of delegate to serialize.</typeparam> |
| | 120 | | /// <param name="delegate">The delegate to serialize.</param> |
| | 121 | | /// <param name="xmlWriterSettings">The settings of the XML writer during serialization.</param> |
| | 122 | | /// <returns>The XML serialization of the delegate.</returns> |
| | 123 | | /// <exception cref="NotSupportedException"> |
| | 124 | | /// Thrown when the delegate is pointing to a non-static method. |
| | 125 | | /// </exception> |
| | 126 | | /// <exception cref="NotSupportedException"> |
| | 127 | | /// Thrown when the delegate is pointing to a local function. |
| | 128 | | /// </exception> |
| | 129 | | public static string StaticDelegateToXml<T>(T @delegate, XmlWriterSettings xmlWriterSettings) where T : Delegate |
| 7 | 130 | | { |
| 7 | 131 | | using StringWriter stringWriter = new(); |
| 7 | 132 | | StaticDelegateToXml(@delegate, stringWriter, xmlWriterSettings); |
| 4 | 133 | | return stringWriter.ToString(); |
| 4 | 134 | | } |
| | 135 | |
|
| | 136 | | /// <summary>Serializes a static delegate to XML.</summary> |
| | 137 | | /// <typeparam name="T">The type of delegate to serialize.</typeparam> |
| | 138 | | /// <param name="delegate">The delegate to serialize.</param> |
| | 139 | | /// <param name="textWriter">The text writer to output the XML serialization to.</param> |
| | 140 | | /// <exception cref="NotSupportedException"> |
| | 141 | | /// Thrown when the delegate is pointing to a non-static method. |
| | 142 | | /// </exception> |
| | 143 | | /// <exception cref="NotSupportedException"> |
| | 144 | | /// Thrown when the delegate is pointing to a local function. |
| | 145 | | /// </exception> |
| | 146 | | public static void StaticDelegateToXml<T>(T @delegate, TextWriter textWriter) where T : Delegate => |
| 0 | 147 | | StaticDelegateToXml(@delegate, textWriter, DefaultXmlWriterSettings); |
| | 148 | |
|
| | 149 | | /// <summary>Serializes a static delegate to XML.</summary> |
| | 150 | | /// <typeparam name="T">The type of delegate to serialize.</typeparam> |
| | 151 | | /// <param name="delegate">The delegate to serialize.</param> |
| | 152 | | /// <param name="textWriter">The text writer to output the XML serialization to.</param> |
| | 153 | | /// <param name="xmlWriterSettings">The settings of the XML writer during serialization.</param> |
| | 154 | | /// <exception cref="NotSupportedException"> |
| | 155 | | /// Thrown when the delegate is pointing to a non-static method. |
| | 156 | | /// </exception> |
| | 157 | | /// <exception cref="NotSupportedException"> |
| | 158 | | /// Thrown when the delegate is pointing to a local function. |
| | 159 | | /// </exception> |
| | 160 | | public static void StaticDelegateToXml<T>(T @delegate, TextWriter textWriter, XmlWriterSettings xmlWriterSettings) whe |
| 7 | 161 | | { |
| 7 | 162 | | using XmlWriter xmlWriter = XmlWriter.Create(textWriter, xmlWriterSettings); |
| 7 | 163 | | MethodInfo methodInfo = @delegate.Method; |
| 7 | 164 | | if (methodInfo.IsLocalFunction()) |
| 1 | 165 | | { |
| 1 | 166 | | throw new NotSupportedException("delegates assigned to local functions are not supported"); |
| | 167 | | } |
| 6 | 168 | | if (!methodInfo.IsStatic) |
| 2 | 169 | | { |
| 2 | 170 | | throw new NotSupportedException("delegates assigned to non-static methods are not supported"); |
| | 171 | | } |
| 4 | 172 | | if (methodInfo.DeclaringType is null) |
| 0 | 173 | | { |
| 0 | 174 | | throw new ArgumentException($"{nameof(@delegate)}.{nameof(Delegate.Method)}.{nameof(MethodInfo.DeclaringType)} is |
| | 175 | | } |
| 4 | 176 | | ParameterInfo[] parameterInfos = methodInfo.GetParameters(); |
| 4 | 177 | | xmlWriter.WriteStartElement(StaticDelegateConstants.Name); |
| 4 | 178 | | { |
| 4 | 179 | | xmlWriter.WriteStartElement(StaticDelegateConstants.DeclaringType); |
| 4 | 180 | | xmlWriter.WriteString(methodInfo.DeclaringType.AssemblyQualifiedName); |
| 4 | 181 | | xmlWriter.WriteEndElement(); |
| | 182 | |
|
| 4 | 183 | | xmlWriter.WriteStartElement(StaticDelegateConstants.MethodName); |
| 4 | 184 | | xmlWriter.WriteString(methodInfo.Name); |
| 4 | 185 | | xmlWriter.WriteEndElement(); |
| | 186 | |
|
| 4 | 187 | | xmlWriter.WriteStartElement(StaticDelegateConstants.Parameters); |
| 32 | 188 | | for (int i = 0; i < parameterInfos.Length; i++) |
| 12 | 189 | | { |
| 12 | 190 | | xmlWriter.WriteStartElement(StaticDelegateConstants.ParameterType); |
| 12 | 191 | | xmlWriter.WriteString(parameterInfos[i].ParameterType.AssemblyQualifiedName); |
| 12 | 192 | | xmlWriter.WriteEndElement(); |
| 12 | 193 | | } |
| 4 | 194 | | xmlWriter.WriteEndElement(); |
| | 195 | |
|
| 4 | 196 | | xmlWriter.WriteStartElement(StaticDelegateConstants.ReturnType); |
| 4 | 197 | | xmlWriter.WriteString(methodInfo.ReturnType.AssemblyQualifiedName); |
| 4 | 198 | | xmlWriter.WriteEndElement(); |
| 4 | 199 | | } |
| 4 | 200 | | xmlWriter.WriteEndElement(); |
| 8 | 201 | | } |
| | 202 | |
|
| | 203 | | #endregion |
| | 204 | |
|
| | 205 | | #region From XML |
| | 206 | |
|
| | 207 | | /// <summary>Deserializes a static delegate from XML.</summary> |
| | 208 | | /// <typeparam name="T">The type of the delegate to deserialize.</typeparam> |
| | 209 | | /// <param name="string">The string of XML content to deserialize.</param> |
| | 210 | | /// <returns>The deserialized delegate.</returns> |
| | 211 | | /// <exception cref="NotSupportedException"> |
| | 212 | | /// Thrown when the delegate is pointing to a non-static method. |
| | 213 | | /// </exception> |
| | 214 | | /// <exception cref="NotSupportedException"> |
| | 215 | | /// Thrown when the delegate is pointing to a local function. |
| | 216 | | /// </exception> |
| | 217 | | /// <exception cref="InvalidOperationException"> |
| | 218 | | /// Thrown when deserialization fails due to a return type mis-match. |
| | 219 | | /// </exception> |
| | 220 | | /// <exception cref="Exception"> |
| | 221 | | /// Thrown when deserialization fails. See the inner exception for more information. |
| | 222 | | /// </exception> |
| | 223 | | public static T StaticDelegateFromXml<T>(string @string) where T : Delegate |
| 2 | 224 | | { |
| 2 | 225 | | using StringReader stringReader = new(@string); |
| 2 | 226 | | return StaticDelegateFromXml<T>(stringReader); |
| 2 | 227 | | } |
| | 228 | |
|
| | 229 | | /// <summary>Deserializes a static delegate from XML.</summary> |
| | 230 | | /// <typeparam name="TDelegate">The type of the delegate to deserialize.</typeparam> |
| | 231 | | /// <param name="textReader">The text reader providing the XML to deserialize.</param> |
| | 232 | | /// <returns>The deserialized delegate.</returns> |
| | 233 | | /// <exception cref="NotSupportedException"> |
| | 234 | | /// Thrown when the delegate is pointing to a non-static method. |
| | 235 | | /// </exception> |
| | 236 | | /// <exception cref="NotSupportedException"> |
| | 237 | | /// Thrown when the delegate is pointing to a local function. |
| | 238 | | /// </exception> |
| | 239 | | /// <exception cref="InvalidOperationException"> |
| | 240 | | /// Thrown when deserialization fails due to a return type mis-match. |
| | 241 | | /// </exception> |
| | 242 | | /// <exception cref="Exception"> |
| | 243 | | /// Thrown when deserialization fails. See the inner exception for more information. |
| | 244 | | /// </exception> |
| | 245 | | public static TDelegate StaticDelegateFromXml<TDelegate>(TextReader textReader) where TDelegate : Delegate |
| 2 | 246 | | { |
| 2 | 247 | | string? declaringTypeString = null; |
| 2 | 248 | | string? methodNameString = null; |
| 2 | 249 | | ListArray<string> parameterTypeStrings = new(); |
| 2 | 250 | | string? returnTypeString = null; |
| 2 | 251 | | using (XmlReader xmlReader = XmlReader.Create(textReader)) |
| 2 | 252 | | { |
| 10 | 253 | | while (xmlReader.Read()) |
| 8 | 254 | | { |
| 20 | 255 | | Loop: |
| 20 | 256 | | if (xmlReader.NodeType is XmlNodeType.Element) |
| 16 | 257 | | { |
| 16 | 258 | | switch (xmlReader.Name) |
| | 259 | | { |
| | 260 | | case StaticDelegateConstants.DeclaringType: |
| 2 | 261 | | declaringTypeString = xmlReader.ReadInnerXml(); |
| 2 | 262 | | goto Loop; |
| | 263 | | case StaticDelegateConstants.MethodName: |
| 2 | 264 | | methodNameString = xmlReader.ReadInnerXml(); |
| 2 | 265 | | goto Loop; |
| | 266 | | case StaticDelegateConstants.ParameterType: |
| 6 | 267 | | parameterTypeStrings.Add(xmlReader.ReadInnerXml()); |
| 6 | 268 | | goto Loop; |
| | 269 | | case StaticDelegateConstants.ReturnType: |
| 2 | 270 | | returnTypeString = xmlReader.ReadInnerXml(); |
| 2 | 271 | | goto Loop; |
| | 272 | | } |
| 4 | 273 | | } |
| 8 | 274 | | } |
| 2 | 275 | | } |
| 2 | 276 | | if (methodNameString is null) |
| 0 | 277 | | { |
| 0 | 278 | | throw new ArgumentException("Deserialization failed due to missing name."); |
| | 279 | | } |
| 2 | 280 | | if (declaringTypeString is null) |
| 0 | 281 | | { |
| 0 | 282 | | throw new ArgumentException("Deserialization failed due to missing type."); |
| | 283 | | } |
| 2 | 284 | | if (returnTypeString is null) |
| 0 | 285 | | { |
| 0 | 286 | | throw new ArgumentException("Deserialization failed due to missing return type."); |
| | 287 | | } |
| 2 | 288 | | Type? declaringType = Type.GetType(declaringTypeString); |
| 2 | 289 | | if (declaringType is null) |
| 0 | 290 | | { |
| 0 | 291 | | throw new ArgumentException("Deserialization failed due to an invalid type."); |
| | 292 | | } |
| 2 | 293 | | Type? returnType = Type.GetType(returnTypeString); |
| | 294 | | MethodInfo? methodInfo; |
| 2 | 295 | | if (parameterTypeStrings.Count > 0) |
| 2 | 296 | | { |
| 2 | 297 | | Type[] parameterTypes = new Type[parameterTypeStrings.Count]; |
| 16 | 298 | | for (int i = 0; i < parameterTypes.Length; i++) |
| 6 | 299 | | { |
| 6 | 300 | | Type? parameterType = Type.GetType(parameterTypeStrings[i]); |
| 6 | 301 | | if (parameterType is null) |
| 0 | 302 | | { |
| 0 | 303 | | throw new ArgumentException("Deserialization failed due to an invalid parameter type."); |
| | 304 | | } |
| 6 | 305 | | parameterTypes[i] = parameterType; |
| 6 | 306 | | } |
| 2 | 307 | | methodInfo = declaringType.GetMethod(methodNameString, parameterTypes); |
| 2 | 308 | | } |
| | 309 | | else |
| 0 | 310 | | { |
| 0 | 311 | | methodInfo = declaringType.GetMethod(methodNameString); |
| 0 | 312 | | } |
| 2 | 313 | | if (methodInfo is null) |
| 0 | 314 | | { |
| 0 | 315 | | throw new ArgumentException("The method of the deserialization was not found."); |
| | 316 | | } |
| 2 | 317 | | if (methodInfo.IsLocalFunction()) |
| 0 | 318 | | { |
| 0 | 319 | | throw new NotSupportedException("Delegates assigned to local functions are not supported."); |
| | 320 | | } |
| 2 | 321 | | if (!methodInfo.IsStatic) |
| 0 | 322 | | { |
| 0 | 323 | | throw new NotSupportedException("Delegates assigned to non-static methods are not supported."); |
| | 324 | | } |
| 2 | 325 | | if (methodInfo.ReturnType != returnType) |
| 0 | 326 | | { |
| 0 | 327 | | throw new ArgumentException("Deserialization failed due to a return type mis-match."); |
| | 328 | | } |
| | 329 | | try |
| 2 | 330 | | { |
| 2 | 331 | | return methodInfo.CreateDelegate<TDelegate>(); |
| | 332 | | } |
| 0 | 333 | | catch (Exception exception) |
| 0 | 334 | | { |
| 0 | 335 | | throw new Exception("Deserialization failed.", exception); |
| | 336 | | } |
| 2 | 337 | | } |
| | 338 | |
|
| | 339 | | #endregion |
| | 340 | |
|
| | 341 | | #endregion |
| | 342 | |
|
| | 343 | | #region JSON |
| | 344 | |
|
| | 345 | | /// <summary>An object for the purposes of serializing static delegates.</summary> |
| | 346 | | public static class Json |
| | 347 | | { |
| | 348 | | /// <summary>An object for the purposes of serializing static delegates.</summary> |
| | 349 | | public class Delegate |
| | 350 | | { |
| | 351 | | /// <summary>The assemlby qualified declaring type of the method.</summary> |
| 14 | 352 | | public string? DeclaringType { get; set; } |
| | 353 | | /// <summary>The name of the method.</summary> |
| 14 | 354 | | public string? MethodName { get; set; } |
| | 355 | | /// <summary>The assembly qualified parameter types of the method.</summary> |
| 22 | 356 | | public string[]? ParameterTypes { get; set; } |
| | 357 | | /// <summary>The assembly qualified return type of the method.</summary> |
| 14 | 358 | | public string? ReturnType { get; set; } |
| | 359 | | } |
| | 360 | | } |
| | 361 | |
|
| | 362 | | #region To JSON |
| | 363 | |
|
| | 364 | | /// <summary>Serializes a static delegate to JSON.</summary> |
| | 365 | | /// <typeparam name="T">The type of delegate to serialize.</typeparam> |
| | 366 | | /// <param name="delegate">The delegate to serialize.</param> |
| | 367 | | /// <returns>The JSON serialization of the delegate.</returns> |
| | 368 | | public static string StaticDelegateToJson<T>(T @delegate) where T : Delegate |
| 7 | 369 | | { |
| 7 | 370 | | if (@delegate is null) |
| 0 | 371 | | { |
| 0 | 372 | | throw new ArgumentNullException(nameof(@delegate)); |
| | 373 | | } |
| 7 | 374 | | MethodInfo methodInfo = @delegate.Method; |
| 7 | 375 | | if (methodInfo.DeclaringType is null) |
| 0 | 376 | | { |
| 0 | 377 | | throw new ArgumentException(message: $"{nameof(@delegate)} has a null DeclaringType", paramName: nameof(@delegate) |
| | 378 | | } |
| 7 | 379 | | if (methodInfo.IsLocalFunction()) |
| 1 | 380 | | { |
| 1 | 381 | | throw new NotSupportedException("delegates assigned to local functions are not supported"); |
| | 382 | | } |
| 6 | 383 | | if (!methodInfo.IsStatic) |
| 2 | 384 | | { |
| 2 | 385 | | throw new NotSupportedException("delegates assigned to non-static methods are not supported"); |
| | 386 | | } |
| 4 | 387 | | ParameterInfo[] parameterInfos = methodInfo.GetParameters(); |
| 4 | 388 | | string[] parameterTypes = new string[parameterInfos.Length]; |
| 32 | 389 | | for (int i = 0; i < parameterTypes.Length; i++) |
| 12 | 390 | | { |
| 12 | 391 | | string? assemblyQualifiedName = parameterInfos[i].ParameterType.AssemblyQualifiedName; |
| 12 | 392 | | if (assemblyQualifiedName is null) |
| 0 | 393 | | { |
| 0 | 394 | | throw new NotSupportedException("delegates with generic type definitions are not supported"); |
| | 395 | | } |
| | 396 | | else |
| 12 | 397 | | { |
| 12 | 398 | | parameterTypes[i] = assemblyQualifiedName; |
| 12 | 399 | | } |
| 12 | 400 | | } |
| | 401 | |
|
| 4 | 402 | | Json.Delegate delegateObject = new() |
| 4 | 403 | | { |
| 4 | 404 | | DeclaringType = methodInfo.DeclaringType.AssemblyQualifiedName, |
| 4 | 405 | | MethodName = methodInfo.Name, |
| 4 | 406 | | ParameterTypes = parameterTypes, |
| 4 | 407 | | ReturnType = methodInfo.ReturnType.AssemblyQualifiedName, |
| 4 | 408 | | }; |
| | 409 | |
|
| 4 | 410 | | return JsonSerializer.Serialize(delegateObject); |
| 4 | 411 | | } |
| | 412 | |
|
| | 413 | | #endregion |
| | 414 | |
|
| | 415 | | #region From JSON |
| | 416 | |
|
| | 417 | | /// <summary>Deserializes a static delegate from JSON.</summary> |
| | 418 | | /// <typeparam name="TDelegate">The type of the delegate to deserialize.</typeparam> |
| | 419 | | /// <param name="string">The string of JSON content to deserialize.</param> |
| | 420 | | /// <returns>The deserialized delegate.</returns> |
| | 421 | | public static TDelegate StaticDelegateFromJson<TDelegate>(string @string) where TDelegate : Delegate |
| 2 | 422 | | { |
| 2 | 423 | | Json.Delegate? delegateObject = JsonSerializer.Deserialize<Json.Delegate>(@string); |
| 2 | 424 | | if (delegateObject is null) |
| 0 | 425 | | { |
| 0 | 426 | | throw new ArgumentException(message: $"JSON deserialization resulted in null", paramName: nameof(@string)); |
| | 427 | | } |
| 2 | 428 | | if (delegateObject.DeclaringType is null) |
| 0 | 429 | | { |
| 0 | 430 | | throw new ArgumentException("Deserialization failed due to missing type."); |
| | 431 | | } |
| 2 | 432 | | if (delegateObject.ReturnType is null) |
| 0 | 433 | | { |
| 0 | 434 | | throw new ArgumentException("Deserialization failed due to missing return type."); |
| | 435 | | } |
| 2 | 436 | | if (delegateObject.MethodName is null) |
| 0 | 437 | | { |
| 0 | 438 | | throw new ArgumentException("Deserialization failed due to missing name."); |
| | 439 | | } |
| 2 | 440 | | Type? declaringType = Type.GetType(delegateObject.DeclaringType); |
| 2 | 441 | | if (declaringType is null) |
| 0 | 442 | | { |
| 0 | 443 | | throw new ArgumentException("Deserialization failed due to an invalid type."); |
| | 444 | | } |
| 2 | 445 | | Type? returnType = Type.GetType(delegateObject.ReturnType); |
| | 446 | | MethodInfo? methodInfo; |
| 2 | 447 | | if (delegateObject.ParameterTypes is not null && delegateObject.ParameterTypes.Length > 0) |
| 2 | 448 | | { |
| 2 | 449 | | Type[] parameterTypes = new Type[delegateObject.ParameterTypes.Length]; |
| 16 | 450 | | for (int i = 0; i < parameterTypes.Length; i++) |
| 6 | 451 | | { |
| 6 | 452 | | Type? parameterType = Type.GetType(delegateObject.ParameterTypes[i]); |
| 6 | 453 | | if (parameterType is null) |
| 0 | 454 | | { |
| 0 | 455 | | throw new ArgumentException("Deserialization failed due to an invalid parameter type."); |
| | 456 | | } |
| 6 | 457 | | parameterTypes[i] = parameterType; |
| 6 | 458 | | } |
| 2 | 459 | | methodInfo = declaringType.GetMethod(delegateObject.MethodName, parameterTypes); |
| 2 | 460 | | } |
| | 461 | | else |
| 0 | 462 | | { |
| 0 | 463 | | methodInfo = declaringType.GetMethod(delegateObject.MethodName); |
| 0 | 464 | | } |
| 2 | 465 | | if (methodInfo is null) |
| 0 | 466 | | { |
| 0 | 467 | | throw new ArgumentException("The method of the deserialization was not found."); |
| | 468 | | } |
| 2 | 469 | | if (methodInfo.IsLocalFunction()) |
| 0 | 470 | | { |
| 0 | 471 | | throw new NotSupportedException("Delegates assigned to local functions are not supported."); |
| | 472 | | } |
| 2 | 473 | | if (!methodInfo.IsStatic) |
| 0 | 474 | | { |
| 0 | 475 | | throw new NotSupportedException("Delegates assigned to non-static methods are not supported."); |
| | 476 | | } |
| 2 | 477 | | if (methodInfo.ReturnType != returnType) |
| 0 | 478 | | { |
| 0 | 479 | | throw new ArgumentException("Deserialization failed due to a return type mis-match."); |
| | 480 | | } |
| | 481 | | try |
| 2 | 482 | | { |
| 2 | 483 | | return methodInfo.CreateDelegate<TDelegate>(); |
| | 484 | | } |
| 0 | 485 | | catch (Exception exception) |
| 0 | 486 | | { |
| 0 | 487 | | throw new Exception("Deserialization failed.", exception); |
| | 488 | | } |
| 2 | 489 | | } |
| | 490 | |
|
| | 491 | | #endregion |
| | 492 | |
|
| | 493 | | #endregion |
| | 494 | |
|
| | 495 | | #endregion |
| | 496 | | } |