-
Notifications
You must be signed in to change notification settings - Fork 54
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
Add support for 'params' method arguments #3
Comments
It would be very useful indeed (to me anyway). Did you find how to do it? |
Real method argument is an array, so you can pass variable number of arguments by composing an array:
|
But how would you do a method like "DoSometing(t1,t2)" and "DoSometing(t1, t2, t3, t4)" ? |
It looks like it is possible to handle "params" argument in the same way as it works in C# (when compiler automatically converts method arguments into array): https://stackoverflow.com/questions/627656/determining-if-a-parameter-uses-params-using-reflection-in-c To handle "params" method argument some modifications are needed here: https://github.com/nreco/lambdaparser/blob/master/src/NReco.LambdaParser/InvokeMethod.cs |
I have added support for "Optional" parameters into my InvokeMethod.cs. If its useful I will try and extract it into a pull request based on the latest source, add unit tests etc. AFAIK it didn't break anything. Params would also be very useful, I may try to implement that. Also another thing that I want to do is support the setting of properties / write fields as well as get / read. I propose that could be done by discriminating between == and =. I understand its pushing the boundaries of what this library is supposed to be however IMO there is not too much holding it back from being a full interpreter for use with rules engines and the likes. Maybe we can add settings to allow disallow this behaviour if security is a concern. We could even add attributes that can decorate api to control access to members? |
This sounds good! Only notice, please ensure that "Optional" parameters don't break compatibility with legacy .NET versions. It might be possible that "Optional" parameters can be supported only in "netstandard2.0" build, this is not a problem - simply use conditional compilation to exclude this enhancement from legacy builds ("netstandard1.3" and "net45"). Support of "params" might be even more useful than "Optional" parameters. Your PR is very welcome!
Regarding
I think this might be not so easy to implement "setters" as it sounds. You're right that most users of the parser, most likely, don't need that and more than that, this capability can be potentially dangerous (so it is 100% should be controlled via option and disabled by default). Take a look to this issue about local variables: #42 |
Thank you for your reply. Ok I will set about isolating the changes into the new version 1.1, Really good news about the new local variables option. I think that will solve most if not all of our requirements so thank you. |
Hi, I have coded the param feature tonight and it appears to work fine. I have tested several different scenarios with different types, mixed types etc and all appears to work fine including Format (via a proxy call in the test object). I will create a separate pull request for it for review. One note is that to avoid code duplication I did extract a couple of segments of code into private functions. We could inline these if you think performance could be impacted slightly. Let me know what you think. |
@hzy-6 to enable support of 'params' and optional parameters you need to replace default
|
@hzy-6 I cant see what is in your GetContext() method but you need to add an instance of your Funcs class with a name such as "f" to the context object. Then in your expression use "f.sum(1,2,3...) |
|
I hope there is no "f." |
No, currently "params" are supported only for object's method invocation, not for delegates. @microspud I didn't tested this yet, but technically delegate declarations also may have params for instance:
so, potentially, we can also add support for "params" in delegates by replacing |
Thank you. What exactly should we do |
To support "params" for delegates invocations NReco.LambdaParser code needs to be changed, you cannot achieve that in the current version. If you know what to do, you can make necessary changes and propose a PR. Otherwise, you need to wait until @microspud or me finds a time for this change. |
Okay, thank you. I'll learn it first. I hope we can release the function interceptor later |
It is useful to support method calls with variable number of parameters (in c# marked as 'params').
Good example is String.Format.
The text was updated successfully, but these errors were encountered: