NReco Framework Common Infrastructure
This library is used in
NReco Framework
applications.
features
- Lambda expression parser (ability to parse expression represented as string into LINQ expression and compile lambda delegate). Portable (PCL) version is available!
-
Delegate adapters for:
- harmonization .NET delegates of different types with compatible signature
- delegates partial application
- lazy delegate proxy
- Runtime .NET types conversion mechanism (extends .NET component model converters) for transparent conversions:
- generic dictionary and non-generic dictrionary
- generic list and non-generic list
- between delegate types with compatible signatures
- other useful conversions
- Free and open source (LGPL)
links
Nuget packages |
NReco Lib (.NET Framework 4.0)
NReco.LambdaParser (PCL, .NET Core) |
Source code | NReco Lib on GitHub NReco LambdaParser on GitHub |
Documentation | NReco Lib API Reference |
how to use
Install NReco nuget package and use one of the following code snippets:
Convert Hashtable into generic dictionary:
var hashMap = new Hashtable() { { "a", 1} }; var genericHashMapWrapper = NReco.Converting.ConvertManager.ChangeType( h, typeof(IDictionary<string,object>) );
Convert func delegates:
Func<int,string> f1 = (i) => { return i.ToString(); }; var f2 = NReco.Converting.ConvertManager.ChangeType( f1, typeof(Func<string,object>) ); Console.WriteLine(f2("1")); // --> 1 f2( "a" ); // --> runtime convert exception
Delegate partial application:
Func<int,int,int> f1 = (a,b) => { return a/b; }; var f2 = (new NReco.PartialDelegateAdapter(f1, new[] { NReco.PartialDelegateAdapter.KeepArg, 2 })).GetDelegate<Func<int,int>>(); Console.WriteLine(f2(2)); // --> 4
Dynamic lambda parser:
var lambdaParser = new NReco.LambdaParser(); Console.WriteLine( lambdaParser.Eval("1+2", new Dictionary<string,object>() ) ); // --> 3 var varContext = new Dictionary<string,object>(); varContext["pi"] = 3.14M; varContext["one"] = 1M; varContext["two"] = 2M; varContext["test"] = "test"; Console.WriteLine( lambdaParser.Eval("pi>one && 0<one ? (1+8)/3+1*two : 0", varContext) ); // --> 5More examples: LambdaParserTests