void Save <T>(T property, RegistryKey key, Enum name) { object value = property; if (Equals <T, bool>()) { value = ((bool)(object)property) ? 1 : 0; } else if (Equals <T, Timeout>()) { value = Convert.ToInt32(property); } else if (typeof(T).IsEnum) { value = Enum.GetName(typeof(T), property); } key.SetValue(name.Cast <string>(), value); }
void Load <T>(Expression <Func <T> > propertyByRef, RegistryKey key, Enum name) { var propertyExpr = (MemberExpression)propertyByRef.Body; var property = (PropertyInfo)propertyExpr.Member; var regValue = key.GetValue(name.Cast <string>()); if (Equals <T, bool>() && regValue is int numValue) { property.SetValue(this, numValue == 1); } else if (Equals <T, Timeout>() && regValue is int timeout) { property.SetValue(this, (Timeout)timeout); } else if (typeof(T).IsEnum && regValue is string enumValue) { property.SetValue(this, Enum.Parse(typeof(T), enumValue)); } else if (regValue is T value) { property.SetValue(this, value); } }
/// <summary> /// Compare enum value with instance/value of type T. /// </summary> /// <typeparam name="T">Cast/comparison type.</typeparam> /// <param name="valueT">Instance/value of type T to compare with.</param> /// <param name="valueEnum">Enum value to compare with.</param> /// <returns>true if cast of valueEnum is equal to valueT, false otherwise</returns> public static bool EqualTo <T>(this T valueT, Enum valueEnum) { return(valueT.Equals(valueEnum.Cast <T>())); }
string ParamKey(Enum param) { return(string.Format("${0}$", param.Cast <string>())); }