0

I am trying to use C# Expression Trees for a rule engine and ran into an issue with Version comparison. I would like the expression to do something like

Version < "4.5.1"

but C# Expressions consider the version value as string and do not allow lessthan operator on it. I could create a custom method for this comparison by pre-identifying the string value as a Version number (RegEx comparison) but I was hoping to find a way where C# Expression can identify is directly.

I was not able to find any documentation or examples on how I can create an Expression that could compare versions.

Any help here would be appreciated

Rahul
  • 1
  • 1
    Version objects are `IComparable` so you can call [Version.CompareTo](https://msdn.microsoft.com/en-us/library/system.version.compareto%28v=vs.110%29.aspx) to it. [like](https://stackoverflow.com/questions/7568147/compare-version-numbers-without-using-split-function) – Frederick Aug 05 '15 at 19:15
  • As expressions you can do somthing like: `var compareVersion = typeof(Version).GetMethod("CompareTo", new Type[] { typeof(Version) }); var versionCtor = typeof(Version).GetConstructor(new Type[] { typeof(string) }); var versionVariable = Expression.Constant(new Version(v1), typeof(Version)); var stringVariable = Expression.Constant(v2, typeof(string)); var comparisonExpression = Expression.Call(versionVariable, compareVersion, Expression.New(versionCtor, stringVariable)); var comparison = Expression.Lambda>(comparisonExpression).Compile(); ` – Frederick Aug 05 '15 at 19:29

0 Answers0