2021-09-05 10:05:04 +02:00
|
|
|
|
using System.Reflection;
|
2021-07-18 14:29:41 +02:00
|
|
|
|
using System.Text.RegularExpressions;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
|
|
|
|
namespace Tapeti.Flow.FlowHelpers
|
|
|
|
|
{
|
2019-08-15 11:26:55 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a method into a unique string representation.
|
|
|
|
|
/// </summary>
|
2017-01-31 12:01:08 +01:00
|
|
|
|
public static class MethodSerializer
|
|
|
|
|
{
|
2019-08-15 11:26:55 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a method into a unique string representation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="method"></param>
|
2017-01-31 12:01:08 +01:00
|
|
|
|
public static string Serialize(MethodInfo method)
|
|
|
|
|
{
|
|
|
|
|
return method.Name + '@' + method.DeclaringType?.Assembly.GetName().Name + ':' + method.DeclaringType?.FullName;
|
|
|
|
|
}
|
2021-07-18 14:29:41 +02:00
|
|
|
|
|
|
|
|
|
|
2022-11-17 16:47:07 +01:00
|
|
|
|
private static readonly Regex DeserializeRegex = new("^(?<method>.+?)@(?<assembly>.+?):(?<type>.+?)$");
|
2021-07-18 14:29:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deserializes the serialized method representation back into it's MethodInfo, or null if not found.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="serializedMethod"></param>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
public static MethodInfo? Deserialize(string serializedMethod)
|
2021-07-18 14:29:41 +02:00
|
|
|
|
{
|
|
|
|
|
var match = DeserializeRegex.Match(serializedMethod);
|
|
|
|
|
if (!match.Success)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
Assembly assembly;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
assembly = Assembly.Load(match.Groups["assembly"].Value);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var declaringType = assembly.GetType(match.Groups["type"].Value);
|
|
|
|
|
return declaringType?.GetMethod(match.Groups["method"].Value);
|
|
|
|
|
}
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
}
|