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

[2.1.0.293,3.0)
[1.1.0.34,2.0)
(1.1.0.34,2.0]
2.1

For parse the reference version string, we should make some property.


public class ReferenceVersion
{
public ReferenceVersion(Version version)
{
Version = version;
MinVersion = version;
MaxVersion = version;
IsIncludeMaxVersion = true;
IsIncludeMinVersion = true;
} public ReferenceVersion(Version minVersion, Version maxVersion, bool isIncludeMinVersion,
bool isIncludeMaxVersion)
{
Version = null;
MinVersion = minVersion;
MaxVersion = maxVersion;
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)
{
if (_regex == null)
{
_regex = new Regex(@"([(|\[])([\d|.]*),([\d|.]*)([)|\]])", RegexOptions.Compiled);
} var res = _regex.Match(str); if (res.Success)
{
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:

    /// <summary>
/// 引用的版本
/// 用来转换 [2.1.0.293,3.0)、 (1.1.0.3,2.0]、 5.2 的版本
/// </summary>
public class ReferenceVersion
{
/// <summary>
/// 创建引用版本
/// </summary>
/// <param name="version">版本</param>
public ReferenceVersion(Version version)
{
Version = version;
MinVersion = version;
MaxVersion = version;
IsIncludeMaxVersion = true;
IsIncludeMinVersion = true;
} /// <summary>
/// 创建引用版本
/// </summary>
/// <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)
{
Version = null;
MinVersion = minVersion;
MaxVersion = maxVersion;
IsIncludeMinVersion = isIncludeMinVersion;
IsIncludeMaxVersion = isIncludeMaxVersion;
} /// <summary>
/// 版本
/// </summary>
public Version Version { get; } /// <summary>
/// 最低版本
/// </summary>
public Version MinVersion { get; } /// <summary>
/// 最高版本
/// </summary>
public Version MaxVersion { get; } /// <summary>
/// 是否包括最低版本
/// </summary>
public bool IsIncludeMinVersion { get; } /// <summary>
/// 是否包括最高版本
/// </summary>
public bool IsIncludeMaxVersion { get; } /// <summary>
/// 转换版本
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static ReferenceVersion Parser(string str)
{
if (_regex == null)
{
_regex = new Regex(@"([(|\[])([\d|.]*),([\d|.]*)([)|\]])", RegexOptions.Compiled);
} var res = _regex.Match(str); if (res.Success)
{
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;
}

我搭建了自己的博客 https://blog.lindexi.com/ 欢迎大家访问,里面有很多新的博客。只有在我看到博客写成熟之后才会放在csdn或博客园,但是一旦发布了就不再更新

如果在博客看到有任何不懂的,欢迎交流,我搭建了 dotnet 职业技术学院 欢迎大家加入


本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

最新文章

  1. 用javascript设置和读取cookie的例子
  2. Mac 显示 Finder 隐藏文件
  3. 试用windows Azure
  4. 解决编译报错:Unable to copy file, because it is being used by another process.
  5. 自动化测试实施的几个idea
  6. JDK8新特性之Lambda表达式
  7. testNg官方文档
  8. [原创]使用GCC创建 Windows NT 下的内核DLL
  9. codeforces 604A Uncowed Forces
  10. 遍历json创建树状表(首先的前提条件是要引入jquery的jquery treeTable插件)
  11. Android.mk简单分析
  12. DOM Exception error 类型
  13. jquery取前、后、父、子元素
  14. net core 部署到windows 服务
  15. windows7安装dlib过程中遇到的问题总结以及运行实例效果
  16. C++反汇编(一)
  17. QueryRunner 错误
  18. Tensorflow的Queue读取数据机制
  19. .bat批处理添加Python任务
  20. Bootstrap框架整理

热门文章

  1. JavaScript--轮播图_带计时器
  2. KiCad 安装后没有元件怎么办?
  3. MUI - 关于百度定位
  4. python 操作asdl
  5. 13 -3 jquery选择器和 jquery动画
  6. 2018-2-13-windows-10预览版升级win10-7月29-10240.16384
  7. @雅礼集训01/10 - T1@ matrix
  8. android学习——android 常见的错误 和 解决方法
  9. AtCoder Beginner Contest 078 D ABS
  10. python selenium 获取对象输入的属性值