/// <summary> /// 初始化 <see cref="PropertyHandler"/> /// </summary> /// <param name="property">属性</param> public PropertyHandler(PropertyInfo property) { Property = property; PropertyType = property.PropertyType; Name = property.Name; Get = ComponentServices.GetGeter(property); Set = ComponentServices.GetSeter(property); }
/// <summary> /// 初始化属性操作器 /// </summary> /// <param name="property"> 需要操作的属性 </param> /// <exception cref="Exception"> /// IOC插件异常 <seealso cref="ComponentServices.GetGeter" /> 或 /// <seealso cref="ComponentServices.GetSeter" /> 出现错误. /// </exception> public PropertyHandler(PropertyInfo property) { Name = property.Name; PropertyType = property.PropertyType; Debug.Assert(ComponentServices.GetGeter != null, "ComponentServices.GetGeter != null"); Debug.Assert(ComponentServices.GetSeter != null, "ComponentServices.GetSeter != null"); _getter = ComponentServices.GetGeter(property); _setter = ComponentServices.GetSeter(property); }
private void InitGetSet(out Type type, out Func <object, object> get, out Action <object, object> set) { type = (Member as PropertyInfo)?.PropertyType ?? (Member as FieldInfo)?.FieldType; if (ComponentServices.GetGeter != null) { get = ComponentServices.GetGeter(Member); } else { switch (Member.MemberType) { case MemberTypes.Property: get = ((PropertyInfo)Member).GetValue; break; case MemberTypes.Field: get = ((FieldInfo)Member).GetValue; break; default: get = null; break; } } if (ComponentServices.GetSeter != null) { set = ComponentServices.GetSeter(Member); } else { switch (Member.MemberType) { case MemberTypes.Property: set = ((PropertyInfo)Member).SetValue; break; case MemberTypes.Field: set = ((FieldInfo)Member).SetValue; break; default: set = null; break; } } }
private static void SetValue(object instance, PropertyInfo p, params string[] names) { for (int i = 0, length = names.Length; i < length; i++) { var node = AppSettings[names[i]]; if (!node.HasValue) { continue; } try { var value = ComponentServices.Converter.Convert(node.Value, p.PropertyType); ComponentServices.GetSeter(p)(instance, value); } catch (Exception ex) { Debug.Assert(true, ex.Message); // ignored } return; } }