26ff8d9b4f
Set up repository with legacy .NET Framework 4.8 source (OLD/), new .NET 10 Blazor solution (NEW/), OpenSpec specifications, documentation, and project configuration.
76 lines
3.0 KiB
C#
Executable File
76 lines
3.0 KiB
C#
Executable File
using System;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace WorkerService.Process
|
|
{
|
|
/// <summary>
|
|
/// Action (void function) JSON converter
|
|
/// </summary>
|
|
public class ActionConverter : JsonConverter
|
|
{
|
|
/// <summary>
|
|
/// Writes the JSON representation of the object
|
|
/// </summary>
|
|
/// <param name="writer">The JsonWriter to write to</param>
|
|
/// <param name="value">The value to write</param>
|
|
/// <param name="serializer">The calling serializer</param>
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
{
|
|
if (value is Action action)
|
|
{
|
|
writer.WriteValue($"{action.Method.DeclaringType}.{action.Method.Name}");
|
|
}
|
|
}
|
|
|
|
/// <summary>Reads the JSON representation of the object.</summary>
|
|
/// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param>
|
|
/// <param name="objectType">Type of the object.</param>
|
|
/// <param name="existingValue">The existing value of object being read.</param>
|
|
/// <param name="serializer">The calling serializer.</param>
|
|
/// <returns>The object value.</returns>
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
{
|
|
//Get the method's full path
|
|
string fullMethodPath = (string)reader.Value;
|
|
if (string.IsNullOrEmpty(fullMethodPath))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
//Extract class and method's names
|
|
string className = fullMethodPath.Substring(0, fullMethodPath.LastIndexOf(".", StringComparison.Ordinal));
|
|
string methodName = fullMethodPath.Substring(fullMethodPath.LastIndexOf(".", StringComparison.Ordinal) + 1);
|
|
|
|
//Get the class type
|
|
Type classType = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).FirstOrDefault(t => string.Equals(t.FullName, className, StringComparison.CurrentCultureIgnoreCase));
|
|
if (classType == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
//Get the function's method info
|
|
MethodInfo methodInfo = classType.GetMethod(methodName);
|
|
if (methodInfo == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return (Action)methodInfo.CreateDelegate(typeof(Action));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether this instance can convert the specified object type
|
|
/// </summary>
|
|
/// <param name="objectType">Type of the object</param>
|
|
/// <returns>
|
|
/// Whether or not this instance can convert ot the specified object type
|
|
/// </returns>
|
|
public override bool CanConvert(Type objectType)
|
|
{
|
|
return objectType == typeof(Action);
|
|
}
|
|
}
|
|
}
|