/// <summary> /// 判断指定对象实例是否存在该属性 /// </summary> public static bool HasProperty(object target, string propertyName) { if (target == null || propertyName == null) { return(false); } string[] names = PickPropertyNames(propertyName); int len = names.Length; object child = target; for (int i = 0; i < (len - 1); i++) { IDynaAccessProxy proxy = GetObjectProxy(child); object parent = child; child = proxy.GetProperty(parent, names[i]); if (child == null) { try { child = Activator.CreateInstance(proxy.GetPropertyType(parent, names[i])); } catch (Exception) { return(false); } } } IDynaAccessProxy endProxy = GetObjectProxy(child); return(endProxy.HasProperty(child, names[len - 1])); }
/// <summary> /// 设置指定对象实例的属性值 /// </summary> public static void SetProperty(System.Object target, System.String propertyName, System.Object propertyValue) { if (target == null) { throw new System.ArgumentException("No target specified"); } if (propertyName == null) { throw new System.ArgumentException("No name specified"); } string[] names = PickPropertyNames(propertyName); int len = names.Length; object child = target; for (int i = 0; i < (len - 1); i++) { IDynaAccessProxy proxy = GetObjectProxy(child); object parent = child; child = proxy.GetProperty(parent, names[i]); if (child == null) { try { child = Activator.CreateInstance(proxy.GetPropertyType(parent, names[i])); proxy.SetProperty(parent, names[i], child); } catch (Exception e) { throw new DynaAccessException(e); } } } IDynaAccessProxy setProxy = GetObjectProxy(child); setProxy.SetProperty(child, names[len - 1], propertyValue); }