/// <summary> /// Method to create a deep or shallow copy of this object /// </summary> public EntryValue Clone(bool deep) { // All value types can be simply copied var copy = new EntryValue { Type = Type, UnitType = UnitType, Current = Current, Default = Default, IsReadOnly = IsReadOnly }; if (deep) { // In a deep clone the references are cloned if (Possible != null) { var tempPossible = new string[Possible.Length]; for (var i = 0; i < Possible.Length; i++) { var value = Possible[i]; tempPossible[i] = value; } copy.Possible = tempPossible; } } else { // In a shallow clone only references are copied copy.Possible = Possible; } return(copy); }
/// <summary> /// Create object for primitive values like int/string or enums /// </summary> private static object CreatePrimitiveOrEnum(Type propertyType, EntryValue entryValue, IFormatProvider formatProvider) { object item; // All value types if (entryValue.Type < EntryValueType.Enum) { // Create value type item = ToObject(entryValue.Type, entryValue.Current, formatProvider); } else { var elementType = ElementType(propertyType); item = ToObject(elementType, entryValue.Current, formatProvider); } return(item); }
/// <see cref="ICustomSerialization"/> private static EntryValue CreateEntryValue(PropertyInfo property, ICustomSerialization customSerialization) { // Set if the current entry is readonly by checking if the property has a setter // or the ReadOnlyAttribute was set to true var isReadOnly = !property.CanWrite; if (!isReadOnly) { var readOnlyAtt = property.GetCustomAttribute <ReadOnlyAttribute>(); isReadOnly = readOnlyAtt?.IsReadOnly ?? false; } // Prepare object var entryValue = new EntryValue { Type = TransformType(property.PropertyType), UnitType = customSerialization.GetUnitTypeByAttributes(property), IsReadOnly = isReadOnly, Possible = customSerialization.PossibleValues(property.PropertyType, property) }; // Get most basic default var defaultAttribute = property.GetCustomAttribute <DefaultValueAttribute>(); if (defaultAttribute != null) { entryValue.Default = defaultAttribute.Value.ToString(); } else if (entryValue.Possible != null && entryValue.Possible.Length >= 1) { entryValue.Default = entryValue.Possible[0]; } else if (property.PropertyType.IsValueType) { entryValue.Default = Activator.CreateInstance(property.PropertyType).ToString(); } // Value types should have the default value as current value if (ValueOrStringType(property.PropertyType)) { entryValue.Current = ConvertToString(entryValue.Default, customSerialization.FormatProvider); } return(entryValue); }
/// <summary> /// Create new entry instance with prefilled properties /// </summary> public Entry() { Value = new EntryValue(); SubEntries = new List <Entry>(); Prototypes = new List <Entry>(); }