Commit d04a2661 authored by 潘栩锋's avatar 潘栩锋 🚴

本来想添加多级绑定功能,像Binding 一样。 但GetValue( obj, new object[]{index}); 失败。 放弃!

parent d91e9ad3
...@@ -3,6 +3,7 @@ using System.Collections.Generic; ...@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace Misc namespace Misc
{ {
...@@ -28,6 +29,8 @@ namespace Misc ...@@ -28,6 +29,8 @@ namespace Misc
/// <param name="v"></param> /// <param name="v"></param>
public static void SetValue(object obj, string propertyName, object v) public static void SetValue(object obj, string propertyName, object v)
{ {
//if (!getRootProperty(ref obj, ref propertyName))
// return;
PropertyInfo property = obj.GetType().GetProperty(propertyName); PropertyInfo property = obj.GetType().GetProperty(propertyName);
if (property != null) if (property != null)
{ {
...@@ -35,6 +38,36 @@ namespace Misc ...@@ -35,6 +38,36 @@ namespace Misc
} }
} }
static bool getRootProperty(ref object obj, ref string propertyName) {
Regex r = new Regex(@"(\S+)\[(\d+)\]");
string[] lvs = propertyName.Split('.');
if (lvs.Count() > 1)
{
//这个是多级的
for (int i = 0; i < lvs.Count() - 1; i++)
{
PropertyInfo property;
string name = lvs[i];
int index = 0;
var match = r.Match(name);
if (match.Success)
{
name = match.Groups[1].Value;
index = int.Parse(match.Groups[2].Value);
}
property = obj.GetType().GetProperty(name);
if (property == null)
return false;//异常
if(match.Success)
obj = property.GetValue(obj,new object[] { index });
else
obj = property.GetValue(obj);
}
propertyName = lvs.Last();
}
return true;
}
/// <summary> /// <summary>
/// 设定参数(属性)的值,触发属性值变化“通知”,writeVal /// 设定参数(属性)的值,触发属性值变化“通知”,writeVal
/// </summary> /// </summary>
...@@ -43,6 +76,9 @@ namespace Misc ...@@ -43,6 +76,9 @@ namespace Misc
/// <param name="val"></param> /// <param name="val"></param>
public static void SetValue(object obj, string propertyName, string val) public static void SetValue(object obj, string propertyName, string val)
{ {
//if (!getRootProperty(ref obj, ref propertyName))
// return;
PropertyInfo property = obj.GetType().GetProperty(propertyName); PropertyInfo property = obj.GetType().GetProperty(propertyName);
if (property != null) if (property != null)
{ {
...@@ -59,6 +95,9 @@ namespace Misc ...@@ -59,6 +95,9 @@ namespace Misc
/// <returns></returns> /// <returns></returns>
public static object GetValue(object obj, string propertyName) public static object GetValue(object obj, string propertyName)
{ {
//if (!getRootProperty(ref obj, ref propertyName))
// return null;
PropertyInfo property = obj.GetType().GetProperty(propertyName); PropertyInfo property = obj.GetType().GetProperty(propertyName);
if (property != null) if (property != null)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment