/// <summary> /// Creates a BinaryValueWriter, passes it to the func to let the writing take place, and returns the generated contents. /// </summary> /// <param name="writeFunc">The func used to do the actual writing of the contents.</param> /// <param name="useEnumNames">If true, Enums I/O will be done using the Enum's name. If false, /// Enum I/O will use the underlying integer value of the Enum.</param> /// <returns>The BinaryValueWriter contents.</returns> public static byte[] CreateAndWrite(Action <BinaryValueWriter> writeFunc, bool useEnumNames = true) { using (BitStream bs = new BitStream()) { using (BinaryValueWriter w = Create(bs, useEnumNames)) { writeFunc(w); } bs.TrimExcess(); return(bs.GetBuffer()); } }
/// <summary> /// Initializes a new instance of the <see cref="GenericValueReader"/> class. /// </summary> /// <param name="filePath">The path to the file to load.</param> /// <param name="rootNodeName">The name of the root node. Not used by all formats, but should always be included anyways.</param> /// <param name="format">The <see cref="GenericValueIOFormat"/> that defines what output format to use. If null, /// <see cref="GenericValueWriter.DefaultFormat"/> will be used.</param> /// <param name="useEnumNames">Whether or not enum names should be used. If true, enum names will always be used. If false, the /// enum values will be used instead. If null, the default value for the underlying <see cref="IValueWriter"/> will be used.</param> /// <exception cref="ArgumentNullException"><paramref name="filePath"/> is null or empty.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="format"/> contains an invalid value.</exception> GenericValueWriter(string filePath, string rootNodeName, GenericValueIOFormat?format = null, bool?useEnumNames = null) { // Get the encoding format to use GenericValueIOFormat formatToUse; if (!format.HasValue) { formatToUse = DefaultFormat; } else { formatToUse = format.Value; } // Create the IValueWriter of the needed type switch (formatToUse) { case GenericValueIOFormat.Binary: if (useEnumNames.HasValue) { _writer = BinaryValueWriter.Create(filePath, useEnumNames.Value); } else { _writer = BinaryValueWriter.Create(filePath); } break; case GenericValueIOFormat.Xml: if (useEnumNames.HasValue) { _writer = XmlValueWriter.Create(filePath, rootNodeName, useEnumNames.Value); } else { _writer = XmlValueWriter.Create(filePath, rootNodeName); } break; default: const string errmsg = "Invalid GenericValueIOFormat value `{0}`."; Debug.Fail(string.Format(errmsg, format)); throw new ArgumentOutOfRangeException("format", string.Format(errmsg, format)); } Debug.Assert(_writer != null); }