diff --git a/ReactWithDotNet/ReflectionHelper.cs b/ReactWithDotNet/ReflectionHelper.cs index e82aacb0..7ee552af 100644 --- a/ReactWithDotNet/ReflectionHelper.cs +++ b/ReactWithDotNet/ReflectionHelper.cs @@ -158,7 +158,7 @@ public static T DeepCopy(T value) return (T)JsonSerializer.Deserialize(json, value.GetType(), JsonSerializerOptionsInstance); } - + public static MethodInfo FindMethod(this Type type, string methodName, BindingFlags bindingFlags) { while (type != null) @@ -240,71 +240,68 @@ internal static class PrimitiveTypeCache public static readonly Type Type_ulong = typeof(ulong); public static readonly Type Type_ushort = typeof(ushort); } - - - } - internal static class SerializationHelperForCompilerGeneratedClasss +static class SerializationHelperForCompilerGeneratedClasss +{ + public static object Deserialize(Type compilerGeneratedType, IReadOnlyDictionary scope) { - public static object Deserialize(Type compilerGeneratedType, IReadOnlyDictionary scope) + var instance = Activator.CreateInstance(compilerGeneratedType); + + foreach (var fieldInfo in compilerGeneratedType.GetFields()) { - var instance = Activator.CreateInstance(compilerGeneratedType); + if (fieldInfo.Name.Contains("__this")) + { + string[] errorMessage = + [ + Environment.NewLine, + "Invalid using of state.", + $"{compilerGeneratedType.FullName} should not be refer {fieldInfo.FieldType.FullName}", + "You should be focus to:", + string.Join(", ", compilerGeneratedType.GetFields().Select(f => f.Name)), + Environment.NewLine + ]; + throw DeveloperException(string.Join(Environment.NewLine, errorMessage)); + } - foreach (var fieldInfo in compilerGeneratedType.GetFields()) + if (scope.TryGetValue(fieldInfo.Name, out var fieldValue)) { - if (fieldInfo.Name.Contains("__this")) - { - string[] errorMessage = - [ - Environment.NewLine, - "Invalid using of state.", - $"{compilerGeneratedType.FullName} should not be refer {fieldInfo.FieldType.FullName}", - "You should be focus to:", - string.Join(", ", compilerGeneratedType.GetFields().Select(f => f.Name)), - Environment.NewLine - ]; - throw DeveloperException(string.Join(Environment.NewLine, errorMessage)); - } - - if (scope.TryGetValue(fieldInfo.Name, out var fieldValue)) + fieldValue = ArrangeValueForTargetType(fieldValue, fieldInfo.FieldType); + + if (fieldValue is MulticastDelegate multicastDelegate) { - fieldValue = ArrangeValueForTargetType(fieldValue, fieldInfo.FieldType); - - if (fieldValue is MulticastDelegate multicastDelegate) - { - fieldValue = Delegate.CreateDelegate(multicastDelegate.GetType(), instance, multicastDelegate.Method); - } - - fieldInfo.SetValue(instance, fieldValue); + fieldValue = Delegate.CreateDelegate(multicastDelegate.GetType(), instance, multicastDelegate.Method); } - } - return instance; + fieldInfo.SetValue(instance, fieldValue); + } } - - public static IReadOnlyDictionary Serialize(object compilerGeneratedTypeInstance) + + return instance; + } + + public static IReadOnlyDictionary Serialize(object compilerGeneratedTypeInstance) + { + var compilerGeneratedType = compilerGeneratedTypeInstance.GetType(); + + return compilerGeneratedType.GetFields().Select(toNameValuePair).ToDictionary(x => x.name, v => v.value); + + (string name, object value) toNameValuePair(FieldInfo f) { - var compilerGeneratedType = compilerGeneratedTypeInstance.GetType(); + var name = f.Name; - return compilerGeneratedType.GetFields().Select(toNameValuePair).ToDictionary(x => x.name, v => v.value); + var value = f.GetValue(compilerGeneratedTypeInstance); - (string name, object value) toNameValuePair(FieldInfo f) + if (value is MulticastDelegate multicastDelegate) { - var name = f.Name; - - var value = f.GetValue(compilerGeneratedTypeInstance); - - if (value is MulticastDelegate multicastDelegate) + if (multicastDelegate.Target == compilerGeneratedTypeInstance) { - if (multicastDelegate.Target == compilerGeneratedTypeInstance) - { - // for avoid circular reference - value = Delegate.CreateDelegate(multicastDelegate.GetType(), null, multicastDelegate.Method); - } + // for avoid circular reference + value = Delegate.CreateDelegate(multicastDelegate.GetType(), null, multicastDelegate.Method); } - - return (name, value); } + + return (name, value); } - } \ No newline at end of file + } +} \ No newline at end of file