Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/custom member access #346

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ANerdyElva
Copy link

We have a project that requires running expressions on mostly configuration based objects (in reality they're nested dictionaries with per-member/per-dict configuration of what variables are stored inside, with type information).
These expressions may in the future be written by people with limited technical experience, so field * 5 is easy to explain but (int)data["field"] * 5 is harder, and it gets worse from there ((int)((List<Dictionary<string,object>>)this["child"])[0].field vs child.field)

We could use dynamic objects but that would require generating a lot of them as it's a complex hierarchical data structure, and this would lose type information, as well as performance.

Instead I've made a small interface that serves as an extension point, if it's used then on attempting to resolve member access it will first attempt to call bool TryGetMemberAccess(Expression leftHand, string identifier, out Expression result).
If it returns false normal code flow resumes, if true is returned result should be an Expression that represents the member access.

This can of course be anything, in the unit tests it generates a dictionary access and a cast.
Case insensitivity must be implemented by the implementer of IMemberAccessProvider if they with to use case insensitivity.

@metoule
Copy link
Contributor

metoule commented Mar 8, 2025

To be honest, that looks like a fairly niche requirement. We could imagine providing support for custom resolution, but we'd want to support more than member access: we'd probably want to support everything handled by the MemberFinder.

Would it be possible for you to preprocess the string instead? If I understand your use case correctly, you could use Detector.DetectIdentifiers to find field and child, and replace them with a parameter with the expected value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants