using System.Reflection; using System.Runtime.CompilerServices; namespace Connected.Interop { public static class Properties { public static PropertyInfo? GetPropertyAttribute(object instance) where T : Attribute { var props = GetProperties(instance, false); if (props is null || !props.Any()) return default; foreach (var property in props) { if (property.GetCustomAttribute() is not null) return property; } return default; } public static PropertyInfo[]? GetProperties(object instance, bool writableOnly) { if (instance.GetType().GetProperties() is not PropertyInfo[] properties) return default; var temp = new List(); foreach (var i in properties) { var getMethod = i.GetGetMethod(); var setMethod = i.GetSetMethod(); if (writableOnly && setMethod is null) continue; if (getMethod is null) continue; if (getMethod is not null && getMethod.IsStatic || setMethod is not null && setMethod.IsStatic) continue; if (setMethod is not null && !setMethod.IsPublic) continue; temp.Add(i); } return temp.ToArray(); } public static IEnumerable GetInheritedProperites(this Type type) { foreach (var info in type.GetInheritedTypeInfos()) { foreach (var p in info.GetRuntimeProperties()) yield return p; } } public static List GetImplementedProperties(object component) { var type = component is Type ct ? ct : component.GetType(); var properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty); var result = new List(); foreach (var property in properties) { if (property.GetCustomAttribute() is not null) continue; result.Add(property); } return result; } public static void SetPropertyValue(object instance, string propertyName, object? value) { var property = instance.GetType().GetProperty(propertyName); if (property is null) return; if (!property.CanWrite) { if (property.DeclaringType is null) return; property = property.DeclaringType.GetProperty(propertyName); } if (property is null || property.SetMethod is null) return; property.SetMethod.Invoke(instance, new object[] { value }); } public static T? FindAttribute(this PropertyInfo info) where T : Attribute { var atts = info.GetCustomAttributes(true); if (atts is null || !atts.Any()) return default; return atts.ElementAt(0); } } }