/// <summary> /// Установить значение свойства propertyName у объекта /// </summary> /// <param name="obj">объект</param> /// <param name="propertyName">имя свойства</param> /// <param name="bindingFlags">условие поиска свойства</param> /// <param name="accessAttr">доступ к селектору свойства</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="Exception"></exception> /// <returns></returns> public static void SetPropertyValue(this object obj, string propertyName, object value, BindingFlags bindingFlags, AccessAttr accessAttr) { PropertyInfo propertyInfo = obj.GetProperty(propertyName, bindingFlags); obj.SetPropertyValue(propertyInfo, value, accessAttr); }
/// <summary> /// Получить значение свойства propertyInfo у объекта /// </summary> /// <param name="obj">объект</param> /// <param name="propertyInfo">свойство</param> /// <param name="accessAttr">доступ к селектору свойства</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="Exception"></exception> /// <returns>значение свойства</returns> public static object GetPropertyValue(this object obj, PropertyInfo propertyInfo, AccessAttr accessAttr) { Type type = obj.GetType(); if (propertyInfo == null) { ExceptionGenerator.Run <ArgumentNullException>("Ошибка при получении значения свойства: свойство объекта типа \"{0}\" неопределено", type); } try { MethodInfo method = null; if (accessAttr == AccessAttr.All) { method = propertyInfo.GetGetMethod(); if (method == null) { method = propertyInfo.GetGetMethod(true); } return(method.Invoke(obj, null)); } method = propertyInfo.GetGetMethod(accessAttr == AccessAttr.NonPublic); return(method.Invoke(obj, null)); } catch (Exception ex) { throw ExceptionGenerator.Run(ex, "Ошибка при получении значения свойства \"{0}\" объекта типа \"{1}\"", propertyInfo.Name, type); } }
/// <summary> /// Получить вложенный тип /// </summary> /// <param name="obj">объект, у которого получает вложенный тип</param> /// <param name="nestedTypeName">имя вложенного типа</param> /// <param name="accessAttr">доступ к вложенному типу</param> /// <returns></returns> public static Type GetNestedType(this object obj, string nestedTypeName, AccessAttr accessAttr) { Type nestedType = obj.GetNestedType(nestedTypeName, accessAttr == AccessAttr.NonPublic ? BindingFlags.NonPublic : BindingFlags.Public); if (nestedType == null) { if (accessAttr == AccessAttr.All) { return(obj.GetNestedType(nestedTypeName, BindingFlags.NonPublic)); } return(null); } return(nestedType); }
/// <summary> /// Установить значение свойства propertyName у объекта /// </summary> /// <param name="obj">объект</param> /// <param name="propertyInfo">свойство</param> /// <param name="accessAttr">доступ к селектору свойства</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="Exception"></exception> /// <returns></returns> public static void SetPropertyValue(this object obj, PropertyInfo propertyInfo, object value, AccessAttr accessAttr) { Type type = obj.GetType(); if (propertyInfo == null) { ExceptionGenerator.Run <ArgumentNullException>("Ошибка при установке значения свойства: свойство объекта типа \"{0}\" неопределено", type); } try { if (accessAttr == AccessAttr.All) { MethodInfo method = propertyInfo.GetSetMethod(); if (method == null) { method = propertyInfo.GetSetMethod(true); } object[] args = new object[1] { value }; method.Invoke(obj, args); } else { MethodInfo method = propertyInfo.GetSetMethod(accessAttr == AccessAttr.NonPublic); object[] args = new object[1] { value }; method.Invoke(obj, args); } } catch (Exception ex) { throw ExceptionGenerator.Run(ex, "Ошибка при установке значения свойства \"{0}\" объекта типа \"{1}\"", propertyInfo.Name, type); } }