WinReflectTest.xaml.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Shapes;
  15. using FWindSoft.Tools;
  16. namespace Test.ReflectTest
  17. {
  18. /// <summary>
  19. /// WinReflectTest.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class WinReflectTest : Window
  22. {
  23. public WinReflectTest()
  24. {
  25. InitializeComponent();
  26. Class2 c = new Class2();
  27. //var result = ObjectUtil.GetPropertyValue(c, "C1.Name");
  28. //var result2 = ObjectUtil.GetPropertyValue(c, "CList[2].Name[0]");
  29. var result3 = ObjectUtil.GetPropertyValue(c, "StrList[1][2]");
  30. //TestRegex();
  31. }
  32. public void TestRegex()
  33. {
  34. string str = "aaaaaaa.[bbbbbb][jlkoihj]";
  35. string pattern = @"(\[.*?\])";//匹配模式
  36. Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
  37. MatchCollection matches = regex.Matches(str);
  38. StringBuilder sb = new StringBuilder();//存放匹配结果
  39. foreach (Match match in matches)
  40. {
  41. string value = match.Value.Trim('[', ']');
  42. sb.AppendLine(value);
  43. }
  44. MessageBox.Show(sb.ToString());
  45. }
  46. }
  47. public class Class1
  48. {
  49. public string Name { get; set; }
  50. }
  51. public class Class2
  52. {
  53. public Class2()
  54. {
  55. C1=new Class1(){Name = "1"};
  56. CList=new List<Class1>();
  57. for (int i = 0; i < 10; i++)
  58. {
  59. CList.Add(new Class1(){Name = "Child"+(i).ToString()});
  60. }
  61. StrList=new List<string>();
  62. StrList.Add("abcdef");
  63. StrList.Add("123456");
  64. }
  65. public Class1 C1 { get; set; }
  66. public List<Class1> CList { get; set; }
  67. public List<string> StrList { get; set; }
  68. }
  69. }