public static void SetPropertyValue <TClass>(TClass @class, string propertyName, object value) where TClass : class { foreach (PropertyInfo property in GetProperties <TClass>()) { if (property.GetCustomAttribute <IgnoreAttribute>() != null) { continue; } IdAttribute id = property.GetCustomAttribute <IdAttribute>(); AliasesAttribute aliases = property.GetCustomAttribute <AliasesAttribute>(); if (string.Equals(property.Name, propertyName, StringComparison.OrdinalIgnoreCase) || (aliases?.Aliases.Any(x => x == propertyName.ToLower()) ?? false) || (id?.Id?.Equals(propertyName, StringComparison.OrdinalIgnoreCase) ?? false)) { if (property.PropertyType == value?.GetType() || (property.PropertyType == typeof(string) && value == null) || (property.PropertyType == typeof(Nullable) && value == null)) { property.SetValue(@class, value); return; } } } }
public static Type GetPropertyType <TClass>(string propertyName) { foreach (PropertyInfo property in GetProperties <TClass>()) { if (property.GetCustomAttribute <IgnoreAttribute>() != null) { continue; } IdAttribute id = property.GetCustomAttribute <IdAttribute>(); AliasesAttribute aliases = property.GetCustomAttribute <AliasesAttribute>(); if (string.Equals(property.Name, propertyName, StringComparison.OrdinalIgnoreCase) || (aliases?.Aliases.Any(x => x == propertyName.ToLower()) ?? false) || (id?.Id?.Equals(propertyName, StringComparison.OrdinalIgnoreCase) ?? false)) { return(property.PropertyType); } } return(null); }