CSharp.ExpressionParser 1.0.1.30394

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package CSharp.ExpressionParser --version 1.0.1.30394
NuGet\Install-Package CSharp.ExpressionParser -Version 1.0.1.30394
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CSharp.ExpressionParser" Version="1.0.1.30394" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CSharp.ExpressionParser --version 1.0.1.30394
#r "nuget: CSharp.ExpressionParser, 1.0.1.30394"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CSharp.ExpressionParser as a Cake Addin
#addin nuget:?package=CSharp.ExpressionParser&version=1.0.1.30394

// Install CSharp.ExpressionParser as a Cake Tool
#tool nuget:?package=CSharp.ExpressionParser&version=1.0.1.30394

C# Expression Parser

The project provides a simple expression parser that transforms a string into a valid C# expression representing a function. The function can be called with or without a parameter or as part of a LINQ query.

Getting Started

The library contains a static class with 4 methods:

Func<TOutput> ExpressionParser.Parse<TOutput>(string input)
Delegate ExpressionParser.Parse(string input)
Func<TInput, TOutput> ExpressionParser.ParseFor<TInput, TOutput>(string input)
Delegate ExpressionParser.ParseFor<TInput>(string input)

Prerequisites

There is no prerequisite to install and use the methods included in this library.

Installing

You can install the ExpressionParser by downloading it as a NuGet package:

Install-Package CSharp.ExpressionParser

After that, you can just use the call directly from your code. Here is a couple of usage examples:

var result1 = ExpressionParser.Parse<int>("(3 + 2) * 3")(); //result1 should be an integer of value 15
var result2 = ExpressionParser.ParseFor<SomeClass, bool>("Id == 23 && IsActive == true")(instance);  //result2 should be a boolean that the value shoul depend on the instance provided as input

If you don't know the output of the result in advance you can use:

var expression = ExpressionParser.Parse(someStringToBeParsed); //expression will have a delegate returning an object.
var result = expression.DynamicInvoke();  //result will have the result of the expression as an object.

Here are a few samples of expressions that will be accepted by the case above:

"2.0 / 5.0" ==> Expected result: 0.4 (decimal)
"3 + 2 * 3" ==> Expected result: 9 (int)
"1 >= 1" ==> Expected result: true (bool)

This is also supported when an input value is provided, but in this case, the type of the input has to be informed. Here is an example:

var expression = ExpressionParser.ParseFor<SomeClass>(someStringToBeParsed);
var result = expression.DynamicInvoke(instaceOfTypeSomeClass);

More examples can be found in the test project.

Supported Operators

Here is a list of supported operators.

The Tests

A NUnit 3 test project is provided in the solution.
The tests currently provide 100% of code coverage, but they are not complete.
We plan to include more positive test cases and many more negative test cases in the future commits.

Contributing

Please read CONTRIBUTING for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Andre Vianna - Initial work - [AndreVianna]https://github.com/AndreVianna)

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Product Compatible and additional computed target framework versions.
.NET Framework net451 is compatible.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated

1.0.1.30394 - Sign the library with a strong key.
     1.0.1.29845 - Make library compatible with .Net Framework 4.5.1 or higher.
     1.0.1.23396 - Initial RTM package.