Serialize.Linq icon indicating copy to clipboard operation
Serialize.Linq copied to clipboard

Bug fix when a type has only 1 public field.

Open petertorocsik opened this issue 3 years ago • 2 comments

When a type has only 1 public field and the field name has

  • different from the name of the type that contains it then System.ArgumentException is generated at MethodCallExpressionNode ToExpression method

  • the same name of the type that contains it, it is static, and it is instantiated then an infinite cycle occurs

I noticed this error when I would have used the Stream type.

What convention should I create tests (if it necessary) and where should I do it?

Thanks in advance for the reply!

petertorocsik avatar Mar 08 '22 15:03 petertorocsik

Hi, can you give me a sample that would break the current implementation? Thanks.

esskar avatar Apr 16 '22 09:04 esskar

Hi, the code in the first paragraph is the same in both cases.

JsonSerializer jsonSerializer = new(); jsonSerializer.AddKnownTypes(new Type[] { typeof(MyClass) }); ExpressionSerializer expressionSerializer = new(jsonSerializer); MyClass myClass = new(); string serializedText = expressionSerializer.SerializeText(() => myClass.DoSomething(6)); Expression expression = expressionSerializer.DeserializeText(serializedText); //((LambdaExpression)expressionSerializer.DeserializeText(serializedText)).Compile().DynamicInvoke(null);

Infinite cycle case:

public class MyClass { public static MyClass MyClassInstance = new();

public void DoSomething(int number)
{
    //Console.WriteLine(number);
}

}

System.ArgumentException case:

public class MyClass { public int MyInt;

public void DoSomething(int number)
{
    //Console.WriteLine(number);
}

}

Exception:

  • Message: "Method 'Void DoSomething(Int32)' declared on type 'SerializeLinqTest.MyClass' cannot be called with instance of type 'System.Int32'"

  • StackTrace: " at System.Linq.Expressions.Expression.ValidateCallInstanceType(Type instanceType, MethodInfo method)\r\n at System.Linq.Expressions.Expression.ValidateStaticOrInstanceMethod(Expression instance, MethodInfo method)\r\n at System.Linq.Expressions.Expression.ValidateMethodAndGetParameters(Expression instance, MethodInfo method)\r\n at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression arg0)\r\n at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)\r\n at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression[] arguments)\r\n at Serialize.Linq.Nodes.MethodCallExpressionNode.ToExpression(IExpressionContext context) in *\Serialize.Linq\src\Serialize.Linq\Nodes\MethodCallExpressionNode.cs:line 66\r\n at Serialize.Linq.Nodes.LambdaExpressionNode.ToExpression(IExpressionContext context) in *\Serialize.Linq\src\Serialize.Linq\Nodes\LambdaExpressionNode.cs:line 59\r\n at Serialize.Linq.Nodes.ExpressionNode.ToExpression() in ~\Serialize.Linq\src\Serialize.Linq\Nodes\ExpressionNode.cs:line 83\r\n at Serialize.Linq.Serializers.ExpressionSerializer.DeserializeText(String text) in *\Serialize.Linq\src\Serialize.Linq\Serializers\ExpressionSerializer.cs:line 72\r\n at Program.<Main>$(String[] args) in *\Program.cs:line 10"

When you use "() => new MyClass().DoSomething(6)" to both cases than these are working...

petertorocsik avatar Apr 23 '22 15:04 petertorocsik

Thanks. Fixed it.

esskar avatar Aug 28 '22 19:08 esskar