Coder Social home page Coder Social logo

Comments (7)

codingseb avatar codingseb commented on July 26, 2024

Hello, thanks for the bug report and pull request.
Until now, I never used struct with ExpressionEvaluator only classes.
So there are probably some others bugs linked to that.
I will integrate your correction to next release and start some tests about the use of structs

from expressionevaluator.

codingseb avatar codingseb commented on July 26, 2024

I did some tests with structs;
Your pull request correct the specific bug you reported thanks for that.
But for now I struggling with this. As struct are ValueType and are copied when given to methods and as ExpressionEvaluator use reflection (methods) to assign fields and properties, some weird things happend when you imbricate structs for example.
When you assign a value in a sub struct, the struct is copied and the assignation is never reported in the struct hierarchy.

// Set 3 on a copy of the struct2
struct1.struct2.x = 3
// This will not return 3
return struct1.struct2.x;

I will make more investigations. But for now be careful when using struct with ExpressionEvaluator.

from expressionevaluator.

AttilaSzobonya avatar AttilaSzobonya commented on July 26, 2024

I see. : \ I spent some time digging through the code, to find a solution. I think the only way around it is that in case of a value assing we have to trace back and set every field value to the new copies.

So in your example first struct2.x = 3 (which creates a 'new struct2') then struct1.struct2 = 'new struct2'.

This would solve the problem with value types and wouldn't cause problems with reference types as you would just re-set the references.

Unfortunatelly, I don't trivially see how to achieve this in your code.

from expressionevaluator.

codingseb avatar codingseb commented on July 26, 2024

Yes you are right I came to the same kind of solution trace the assignation path.
I just need now to find the way to implement it.

from expressionevaluator.

codingseb avatar codingseb commented on July 26, 2024

I will try it these next couple of days.

from expressionevaluator.

codingseb avatar codingseb commented on July 26, 2024

OK I found a way to manage this.

All is done this tiny class

private class ValueTypeNestingTrace
{
	public object Container { get; set; }

	public MemberInfo Member { get; set; }

	public object Value { get; set; }

	public void AssignValue()
	{
		if (Container is ValueTypeNestingTrace valueTypeNestingTrace)
		{
			((dynamic)Member).SetValue(valueTypeNestingTrace.Value, Value);
			valueTypeNestingTrace.AssignValue();
		}
		else
		{
			((dynamic)Member).SetValue(Container, Value);
		}
	}
}

And some small modifications in the method EvaluateVarOrFunc

I did some tests and actually it works better than C# when doing something like :

classContainer = new ClassContainer()
{
    NestedStructProperty = new StructForTest1()
    {
        myIntvalue = 8,
        myStringValue = "Test"
    }
};

// Do not compile In C#
classContainer.NestedStructProperty.myIntvalue = 9;

// Return : Test 9
return $"Result {classContainer.NestedStructProperty.myStringValue} {classContainer.NestedStructProperty.myIntvalue}";

I don't know if it should be more like C# or if it is better like this
Maybe I will add an option later to choose if this specific case must throw an exception.

For now I will integrate it as this in the next version

from expressionevaluator.

codingseb avatar codingseb commented on July 26, 2024

Just published v : 1.3.7.0

from expressionevaluator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.