diff --git a/src/NReco.LambdaParser/InvokeMethod.cs b/src/NReco.LambdaParser/InvokeMethod.cs index e34fda5..602c600 100644 --- a/src/NReco.LambdaParser/InvokeMethod.cs +++ b/src/NReco.LambdaParser/InvokeMethod.cs @@ -18,54 +18,62 @@ using System.Text; using System.Reflection; -namespace NReco { - +namespace NReco +{ + /// /// Invoke object's method that is most compatible with provided arguments /// - internal class InvokeMethod { + internal class InvokeMethod + { public object TargetObject { get; set; } public string MethodName { get; set; } - public InvokeMethod(object o, string methodName) { + public InvokeMethod(object o, string methodName) + { TargetObject = o; MethodName = methodName; } - protected MethodInfo FindMethod(Type[] argTypes) { - if (TargetObject is Type) { + protected MethodInfo FindMethod(Type[] argTypes) + { + if (TargetObject is Type) + { // static method - #if NET40 +#if NET40 return ((Type)TargetObject).GetMethod(MethodName, BindingFlags.Static | BindingFlags.Public); - #else - return ((Type)TargetObject).GetRuntimeMethod(MethodName, argTypes); - #endif +#else + return ((Type) TargetObject).GetRuntimeMethod(MethodName, argTypes); +#endif } - #if NET40 +#if NET40 return TargetObject.GetType().GetMethod(MethodName, argTypes); - #else +#else return TargetObject.GetType().GetRuntimeMethod(MethodName, argTypes); - #endif +#endif } - protected IEnumerable GetAllMethods() { - if (TargetObject is Type) { - #if NET40 + protected IEnumerable GetAllMethods() + { + if (TargetObject is Type) + { +#if NET40 return ((Type)TargetObject).GetMethods(BindingFlags.Static | BindingFlags.Public); - #else - return ((Type)TargetObject).GetRuntimeMethods(); - #endif +#else + return ((Type) TargetObject).GetRuntimeMethods(); +#endif } - #if NET40 +#if NET40 return TargetObject.GetType().GetMethods(); - #else +#else return TargetObject.GetType().GetRuntimeMethods(); - #endif +#endif } - public object Invoke(object[] args) { + public object Invoke(object[] args) + { Type[] argTypes = new Type[args.Length]; for (int i = 0; i < argTypes.Length; i++) argTypes[i] = args[i] != null ? args[i].GetType() : typeof(object); @@ -73,96 +81,120 @@ public object Invoke(object[] args) { // strict matching first MethodInfo targetMethodInfo = FindMethod(argTypes); // fuzzy matching - if (targetMethodInfo==null) { + if (targetMethodInfo == null) + { var methods = GetAllMethods(); foreach (var m in methods) - if (m.Name==MethodName && - m.GetParameters().Length == args.Length && - CheckParamsCompatibility(m.GetParameters(), argTypes, args)) { + if (m.Name == MethodName && + m.GetParameters().Length == args.Length && + CheckParamsCompatibility(m.GetParameters(), argTypes, args)) + { targetMethodInfo = m; break; } } - if (targetMethodInfo == null) { + + if (targetMethodInfo == null) + { string[] argTypeNames = new string[argTypes.Length]; - for (int i=0; i dictionary && dictionary.TryGetValue(propertyName, out objectValue)) + { + return new LambdaParameterWrapper(objectValue, Cmp); + } + #if NET40 var prop = obj.GetType().GetProperty(propertyName); #else @@ -140,6 +147,7 @@ public LambdaParameterWrapper InvokePropertyOrField(object obj, string propertyN var propVal = prop.GetValue(obj, null); return new LambdaParameterWrapper(propVal, Cmp); } + #if NET40 var fld = obj.GetType().GetField(propertyName); #else @@ -149,6 +157,7 @@ public LambdaParameterWrapper InvokePropertyOrField(object obj, string propertyN var fldVal = fld.GetValue(obj); return new LambdaParameterWrapper(fldVal, Cmp); } + throw new MissingMemberException(obj.GetType().ToString()+"."+propertyName); } diff --git a/src/NReco.LambdaParser/NReco.LambdaParser.csproj b/src/NReco.LambdaParser/NReco.LambdaParser.csproj index 33c1a41..d9caee7 100644 --- a/src/NReco.LambdaParser/NReco.LambdaParser.csproj +++ b/src/NReco.LambdaParser/NReco.LambdaParser.csproj @@ -1,124 +1,76 @@  + + net5.0 + NU1605;114 + - - Runtime expressions parser and evaluator (formulas, conditions, method calls, properties/indexers etc). Builds LINQ expression tree and compiles it to lambda delegate with full support of dynamically typed variables. - Dynamic expressions evaluator - Copyright (c) 2013-2020 Vitalii Fedorchenko - NReco.LambdaParser - 1.0.12 - Vitalii Fedorchenko - netstandard2.0;netstandard1.3;net45 - - true - NReco.LambdaParser - NReco.LambdaParser.snk - False - true - NReco.LambdaParser - LambdaParser;Formula;Parser;Math;Expression;Evaluator;Eval;LINQ-dynamic;PCL;netstandard;netcore;net40;net45;WindowsPhone8;Silverlight - Source code and examples: https://github.com/nreco/lambdaparser -v.1.0.12 changes: -- fixed an issue when comparison result is used as an argument in a method call #30 + + true + false + false + true + false + -v.1.0.11 changes: -- removed legacy Portable targets that cause build problems in some environments - -v.1.0.10 changes: -- fixed issue with 'Object must implement IConvertible' (occurs when delegate argument doesn't implement IConvertible, even if conversion is not needed) - -v.1.0.9 changes: -- defined +/- operations for datetime/timespan types #17 -- added ILambdaValue for accessing real values in Expression produced by LamdbaParser #16 -- added option to allow single equal sign '=' for comparison #13 -- added netstandard2.0 build #15 - -v.1.0.8 changes: -- fixed parse error of calls chain like delegate().method1().method2() - -v.1.0.7 changes: -- fixed evaluation of and/or conditions (exceptions like 'AndAlso / OrElse is not defined') + + true + Runtime expressions parser and evaluator (formulas, conditions, method calls, properties/indexers etc). Builds LINQ expression tree and compiles it to lambda delegate with full support of dynamically typed variables. + Dynamic expressions evaluator + Copyright (c) 2013-2020 Vitalii Fedorchenko + NReco.LambdaParser + 1.0.12.1 + 1.0.12.1 + Vitalii Fedorchenko + + + true + NReco.LambdaParser + NReco.LambdaParser.snk + False + + WillowMedia.NReco.LambdaParser + + Source code and examples: https://github.com/nreco/lambdaparser + v.1.0.12 changes: + - fixed an issue when comparison result is used as an argument in a method call #30 -v.1.0.6 changes: -- introduced IValueComparer interface for custom values comparison (https://github.com/nreco/lambdaparser/issues/10) -- default ValueComparer implementation with options: NullComparison (MinValue, Sql), SuppressErrors -- starting from this version expression cache is instance specific (was global) because calculation result may depend on IValueComparer implementation - - https://www.nrecosite.com/img/nreco-logo-200.png - https://github.com/nreco/lambdaparser - https://raw.githubusercontent.com/nreco/lambdaparser/master/LICENSE - https://github.com/nreco/lambdaparser - git - 1.6.0 - false - false - false - false - false - false - false - false - false - - + v.1.0.11 changes: + - removed legacy Portable targets that cause build problems in some environments - - .NETPortable - v4.5 - Profile259 - .NETPortable,Version=v0.0,Profile=Profile259 - $(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets - - - .NETPortable - v4.0 - Profile328 - .NETPortable,Version=v0.0,Profile=Profile328 - $(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets - $(DefineConstants);NET40 - - - - - - - - - - - - - - - - - + v.1.0.10 changes: + - fixed issue with 'Object must implement IConvertible' (occurs when delegate argument doesn't implement IConvertible, even if conversion is not needed) - - - - - - - - - - - - - - - - - - - - - + v.1.0.9 changes: + - defined +/- operations for datetime/timespan types #17 + - added ILambdaValue for accessing real values in Expression produced by LamdbaParser #16 + - added option to allow single equal sign '=' for comparison #13 + - added netstandard2.0 build #15 - - - + v.1.0.8 changes: + - fixed parse error of calls chain like delegate().method1().method2() - - + v.1.0.7 changes: + - fixed evaluation of and/or conditions (exceptions like 'AndAlso / OrElse is not defined') + + v.1.0.6 changes: + - introduced IValueComparer interface for custom values comparison (https://github.com/nreco/lambdaparser/issues/10) + - default ValueComparer implementation with options: NullComparison (MinValue, Sql), SuppressErrors + - starting from this version expression cache is instance specific (was global) because calculation result may depend on IValueComparer implementation + + https://www.nrecosite.com/img/nreco-logo-200.png + https://github.com/nreco/lambdaparser + https://raw.githubusercontent.com/nreco/lambdaparser/master/LICENSE + https://github.com/nreco/lambdaparser + git + + + + + + + + + + + diff --git a/src/NReco.LambdaParser/Properties/AssemblyInfo.cs b/src/NReco.LambdaParser/Properties/AssemblyInfo.cs index 1115541..40a3e1b 100644 --- a/src/NReco.LambdaParser/Properties/AssemblyInfo.cs +++ b/src/NReco.LambdaParser/Properties/AssemblyInfo.cs @@ -1,30 +1,30 @@ -using System.Resources; -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("NReco.LambdaParser")] -[assembly: AssemblyDescription("Runtime parser for string expressions (formulas, method calls etc). Builds LINQ expression tree and compiles it to lambda delegate, supports dynamically typed variables.")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("Vitalii Fedorchenko")] -[assembly: AssemblyProduct("NReco.LambdaParser")] -[assembly: AssemblyCopyright("Copyright © Vitalii Fedorchenko 2013-2019")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] -[assembly: NeutralResourcesLanguage("en")] - -// Version information for an assembly consists of the following four values: +// using System.Resources; +// using System.Reflection; +// using System.Runtime.CompilerServices; +// using System.Runtime.InteropServices; // -// Major Version -// Minor Version -// Build Number -// Revision +// // General Information about an assembly is controlled through the following +// // set of attributes. Change these attribute values to modify the information +// // associated with an assembly. +// [assembly: AssemblyTitle("NReco.LambdaParser")] +// [assembly: AssemblyDescription("Runtime parser for string expressions (formulas, method calls etc). Builds LINQ expression tree and compiles it to lambda delegate, supports dynamically typed variables.")] +// [assembly: AssemblyConfiguration("")] +// [assembly: AssemblyCompany("Vitalii Fedorchenko")] +// [assembly: AssemblyProduct("NReco.LambdaParser")] +// [assembly: AssemblyCopyright("Copyright © Vitalii Fedorchenko 2013-2019")] +// [assembly: AssemblyTrademark("")] +// [assembly: AssemblyCulture("")] +// [assembly: NeutralResourcesLanguage("en")] // -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.11.0")] -[assembly: AssemblyFileVersion("1.0.11.0")] +// // Version information for an assembly consists of the following four values: +// // +// // Major Version +// // Minor Version +// // Build Number +// // Revision +// // +// // You can specify all the values or you can default the Build and Revision Numbers +// // by using the '*' as shown below: +// // [assembly: AssemblyVersion("1.0.*")] +// [assembly: AssemblyVersion("1.0.11.0")] +// [assembly: AssemblyFileVersion("1.0.11.0")]