Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
beyaz committed Jan 29, 2024
1 parent b2c3694 commit 3fb7eb6
Showing 1 changed file with 48 additions and 51 deletions.
99 changes: 48 additions & 51 deletions ReactWithDotNet/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static T DeepCopy<T>(T value)

return (T)JsonSerializer.Deserialize(json, value.GetType(), JsonSerializerOptionsInstance);
}

public static MethodInfo FindMethod(this Type type, string methodName, BindingFlags bindingFlags)
{
while (type != null)
Expand Down Expand Up @@ -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<string, object> scope)
{
public static object Deserialize(Type compilerGeneratedType, IReadOnlyDictionary<string, object> 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<string, object> Serialize(object compilerGeneratedTypeInstance)

return instance;
}

public static IReadOnlyDictionary<string, object> 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);
}
}
}
}

0 comments on commit 3fb7eb6

Please sign in to comment.