0
0
Ver Fonte

字符串拓展格式化对象功能

zhontai há 3 anos atrás
pai
commit
932db5e834
1 ficheiros alterados com 29 adições e 0 exclusões
  1. 29 0
      Admin.Core.Common/Extensions/StringExtensions.cs

+ 29 - 0
Admin.Core.Common/Extensions/StringExtensions.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Linq;
 using System.Text;
+using System.Text.RegularExpressions;
 using Admin.Core.Common.Helpers;
 
 namespace Admin.Core
@@ -98,5 +99,33 @@ namespace Admin.Core
 
             return s.Replace(@"\", "/");
         }
+
+        public static string Format(this string str, object obj)
+        {
+            if (str.IsNull())
+            {
+                return str;
+            }
+            string s = str;
+            if (obj.GetType().Name == "JObject")
+            {
+                foreach (var item in (Newtonsoft.Json.Linq.JObject)obj)
+                {
+                    var k = item.Key.ToString();
+                    var v = item.Value.ToString();
+                    s = Regex.Replace(s, "\\{" + k + "\\}", v, RegexOptions.IgnoreCase);
+                }
+            }
+            else
+            {
+                foreach (System.Reflection.PropertyInfo p in obj.GetType().GetProperties())
+                {
+                    var xx = p.Name;
+                    var yy = p.GetValue(obj).ToString();
+                    s = Regex.Replace(s, "\\{" + xx + "\\}", yy, RegexOptions.IgnoreCase);
+                }
+            }
+            return s;
+        }
     }
 }