/// <summary> /// Gets a value indicating if conversion of a value to the specified tag type is possible. /// </summary> /// <param name="value">The value to test.</param> /// <param name="type">The target tag type.</param> /// <returns>Returns a value indicating if conversion is possible.</returns> public static bool CanConvert(object value, TagType type) { try { object test = TagType.ConvertTo(value, type); } catch { return(false); } return(true); }
private void MergeCompound(TagCompound original, TagCompound compound, MergeMethod method) { if (method == MergeMethod.KeepSource) { foreach (TagCompound tag in compound.EnumerateCompounds()) { if (!original.ContainsTag(tag.Name)) { // If the compound does not exist in the source structure, then simply add it. original.AddCompound(tag); } else { // Otherwise, continue normal merging with that compound. MergeCompound(compound, tag, method); } } foreach (Tag tag in compound.EnumerateTags()) { // Adds the tag only if it does not exist in the source structure. if (!original.ContainsTag(tag.Name)) { original.AddTag(tag); } } } else if (method == MergeMethod.Overwrite) { foreach (TagCompound tag in compound.EnumerateCompounds()) { if (!original.ContainsTag(tag.Name)) { // If the compound does not exist in the source structure, then simply add it. original.AddCompound(tag); } else { // Otherwise, continue normal merging with that compound. MergeCompound(compound, tag, method); } } foreach (Tag tag in compound.EnumerateTags()) { // If the tag already exists in the source structure, then remove it. if (original.ContainsTag(tag.Name)) { original.RemoveTag(tag.Name); } // Add the merged tag. original.AddTag(tag); } } else if (method == MergeMethod.SourceOnly) { foreach (TagCompound tag in compound.EnumerateCompounds()) { if (original.ContainsTag(tag.Name)) { MergeCompound(compound, tag, method); } } foreach (Tag tag in compound.EnumerateTags()) { // Checks if the tag exists in the source structure. if (original.ContainsTag(tag.Name)) { Tag t = original.GetTag(tag.Name); // Checks if there is a suitable conversion between the two tag types. if (TagType.CanConvert(tag.Value, t.Type)) { // Sets the value of the source tag to the converted value of the tag. t.Value = TagType.ConvertTo(tag.Value, t.Type); } } } } }