Example #1
0
        /// <summary>
        /// Reads an object from the source object's extension dictionary.
        /// </summary>
        /// <typeparam name="T">The type of the object to read.</typeparam>
        /// <param name="source">The source object to read the object from.</param>
        /// <param name="key">A string that acts as the key in the extension dictionary.</param>
        /// <exception cref="System.Exception">Thrown when an AutoCAD error occurs.</exception>
        /// <returns>The object in the extension dictionary.</returns>
        private static T GetFromXExtensionDictionary <T>(DBObject source, string key)
        {
            if (source.ExtensionDictionary.IsNull)
            {
                throw new KeyNotFoundException();
            }

            try
            {
                var items = new TypedValue[0];

                Helpers.WrapInTransaction(source, tr =>
                {
                    var dict = (DBDictionary)tr.GetObject(source.ExtensionDictionary, OpenMode.ForRead);

                    if (dict.Contains(key))
                    {
                        var xRecord = (Xrecord)tr.GetObject(dict.GetAt(key), OpenMode.ForRead);
                        items       = xRecord.Data
                                      .Cast <TypedValue>()
                                      .ToArray();
                    }
                    else
                    {
                        throw Error.KeyNotFound(key);
                    }
                });

                if (items.Length == 1 &&
                    (items[0].TypeCode != (int)DxfCode.BinaryChunk ||
                     typeof(T).Equals(typeof(byte[]))))
                {
                    return((T)items[0].Value);
                }
                else
                {
                    return(Helpers.Deserialize <T>(items.SelectMany(i => (byte[])i.Value)
                                                   .ToArray()));
                }
            }
            catch (Exception e)
            {
                throw Error.AutoCadException(e);
            }
        }