Now we are making a solution that has to get the package reference. But the version of package reference is a range and the default version parser need input a version but not a version range.
This post will tell you how to parse the version range string to reference version.
The format for reference version is like this
For parse the reference version string, we should make some property.
public class ReferenceVersion
public ReferenceVersion ( Version version )
IsIncludeMaxVersion = true ;
IsIncludeMinVersion = true ;
public ReferenceVersion ( Version minVersion , Version maxVersion , bool isIncludeMinVersion ,
bool isIncludeMaxVersion )
IsIncludeMinVersion = isIncludeMinVersion;
IsIncludeMaxVersion = isIncludeMaxVersion;
public Version Version { get ; }
public Version MinVersion { get ; }
public Version MaxVersion { get ; }
public bool IsIncludeMinVersion { get ; }
public bool IsIncludeMaxVersion { get ; }
I will use regex to get the string and parse the string to version.
public static ReferenceVersion Parser ( string str )
_regex = new Regex ( @" ([(|\[])([\d|.]*),([\d|.]*)([)|\]]) " , RegexOptions.Compiled);
var res = _regex. Match (str);
var isIncludeMinVersion = res.Groups[ 1 ].Value;
var minVersion = res.Groups[ 2 ].Value;
var maxVersion = res.Groups[ 3 ].Value;
var isIncludeMaxVersion = res.Groups[ 4 ].Value;
return new ReferenceVersion
string . IsNullOrEmpty (minVersion) ? null : Version. Parse (minVersion),
string . IsNullOrEmpty (maxVersion) ? null : Version. Parse (maxVersion),
isIncludeMinVersion. Equals ( " [ " ),
isIncludeMaxVersion. Equals ( " ] " )
return new ReferenceVersion (Version. Parse (str));
private static Regex _regex ;
We can get the reference version in the solution file and know the solution reference package.
Full code:
/// 用来转换 [2.1.0.293,3.0)、 (1.1.0.3,2.0]、 5.2 的版本
public class ReferenceVersion
/// < param name = " version " > 版本 </ param >
public ReferenceVersion ( Version version )
IsIncludeMaxVersion = true ;
IsIncludeMinVersion = true ;
/// < param name = " minVersion " > 最低版本 </ param >
/// < param name = " maxVersion " > 最高版本 </ param >
/// < param name = " isIncludeMinVersion " > 是否包括最低版本 </ param >
/// < param name = " isIncludeMaxVersion " > 是否包括最高版本 </ param >
public ReferenceVersion ( Version minVersion , Version maxVersion , bool isIncludeMinVersion ,
bool isIncludeMaxVersion )
IsIncludeMinVersion = isIncludeMinVersion;
IsIncludeMaxVersion = isIncludeMaxVersion;
public Version Version { get ; }
public Version MinVersion { get ; }
public Version MaxVersion { get ; }
public bool IsIncludeMinVersion { get ; }
public bool IsIncludeMaxVersion { get ; }
/// < param name = " str " ></ param >
public static ReferenceVersion Parser ( string str )
_regex = new Regex ( @" ([(|\[])([\d|.]*),([\d|.]*)([)|\]]) " , RegexOptions.Compiled);
var res = _regex. Match (str);
var isIncludeMinVersion = res.Groups[ 1 ].Value;
var minVersion = res.Groups[ 2 ].Value;
var maxVersion = res.Groups[ 3 ].Value;
var isIncludeMaxVersion = res.Groups[ 4 ].Value;
return new ReferenceVersion
string . IsNullOrEmpty (minVersion) ? null : Version. Parse (minVersion),
string . IsNullOrEmpty (maxVersion) ? null : Version. Parse (maxVersion),
isIncludeMinVersion. Equals ( " [ " ),
isIncludeMaxVersion. Equals ( " ] " )
return new ReferenceVersion (Version. Parse (str));
private static Regex _regex ;
原文链接: http://blog.lindexi.com/post/How-to-parse-version-range
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。
欢迎转载、使用、重新发布,但务必保留文章署名 林德熙 (包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我 联系 。