Browse Source

增加config读取bool类型转换,增加String扩展方法

yuzhengyang 6 years ago
parent
commit
9dee58a2bc

+ 33 - 0
Azylee.Utils/Azylee.Core/DataUtils/StringUtils/StringExtension.cs

@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DataUtils.StringUtils
+{
+    /// <summary>
+    /// String类型扩展方法
+    /// </summary>
+    public static class StringExtension
+    {
+        private static void Test()
+        {
+            string s = "Hello Extension Methods";
+            int i = s.WordCount();
+        }
+        /// <summary>
+        /// 判断字符串 非null、""、空格(Not NullOrWhiteSpace)
+        /// </summary>
+        /// <param name="s"></param>
+        /// <returns></returns>
+        public static bool Ok(this String s)
+        {
+            return Str.Ok(s);
+        }
+        public static int WordCount(this String str)
+        {
+            return str.Split(new char[] { ' ', '.', '?' },
+                             StringSplitOptions.RemoveEmptyEntries).Length;
+        }
+    }
+}

+ 34 - 0
Azylee.Utils/Azylee.Core/IOUtils/TxtUtils/ConfigTool.cs

@@ -12,8 +12,17 @@ using System.Text;
 
 namespace Azylee.Core.IOUtils.TxtUtils
 {
+    /// <summary>
+    /// .NET 读取配置工具
+    /// </summary>
     public class ConfigTool
     {
+        /// <summary>
+        /// 获取配置值
+        /// </summary>
+        /// <param name="key"></param>
+        /// <param name="defaultValue"></param>
+        /// <returns></returns>
         public static string Get(string key, string defaultValue = "")
         {
             try
@@ -23,6 +32,31 @@ namespace Azylee.Core.IOUtils.TxtUtils
             }
             catch { return defaultValue; }
         }
+        /// <summary>
+        /// 获取配置值(bool)
+        /// </summary>
+        /// <param name="key"></param>
+        /// <param name="defaultValue"></param>
+        /// <returns></returns>
+        public static bool GetBool(string key, bool defaultValue = false)
+        {
+            string value = Get(key, "");
+            if (Str.Ok(value))
+            {
+                value = value.ToLower();
+                switch (value)
+                {
+                    case "1":
+                    case "ok":
+                    case "yes":
+                    case "true":
+                    case "enable":
+                    case "access": return true;
+                    default: return false;
+                }
+            }
+            return defaultValue;
+        }
         public static string GetExe(string exePath, string key, string defaultValue = "")
         {
             try