public T this[int index]
        {
            get
            {
                DebugHelper.AssertUIThread();

                Debug.Assert(this.valueArray != null);

                return(this.valueArray[index]);
            }
            set
            {
                DebugHelper.AssertUIThread();

                Debug.Assert(this.keyValue != null);
                Debug.Assert(this.valueArray != null);

                if (!value.Equals(this.valueArray[index]))
                {
                    this.valueArray[index] = value;

                    MetadataKeyValuePair keyValue = this.keyValue;
                    T[] valueArray = this.valueArray;

                    Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                    {
                        keyValue.Value = valueArray;
                    }));
                }
            }
        }
        public void WriteToBuffer(Type structType, object value)
        {
            DebugHelper.AssertUIThread();

            if (structType != null)
            {
                MetadataKeyValuePair keyValue = this.KeyValue;
                byte[] valueArray             = this.GetValueArray();

                Debug.Assert(keyValue != null);
                Debug.Assert(valueArray != null);

                if (Marshal.SizeOf(structType) > valueArray.Length)
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Struct '{0}' is bigger than the binary metadata for key '{1}'.", structType.FullName, keyValue.Key));
                }

                unsafe
                {
                    fixed(byte *pFoo = valueArray)
                    {
                        IntPtr ptr = new IntPtr(pFoo);

                        Marshal.StructureToPtr(value, ptr, true);
                    }
                }

                Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                {
                    keyValue.Value = valueArray;
                }));
            }
        }
        public MetadataArrayProxy(MetadataKeyValuePair keyValue, ReadOnlyCollection <T> data)
        {
            if (keyValue == null)
            {
                throw new ArgumentNullException("keyValue");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            this.keyValue = keyValue;

            this.valueArray = new T[data.Count];
            data.CopyTo(this.valueArray, 0);
        }
        public object ReadFromBuffer(Type structType)
        {
            DebugHelper.AssertUIThread();

            object value = null;

            if (structType != null)
            {
                if ((this.cachedStruct != null) && (this.cachedStruct.GetType() == structType))
                {
                    value = this.cachedStruct;
                }
                else
                {
                    MetadataKeyValuePair keyValue = this.KeyValue;
                    byte[] valueArray             = this.GetValueArray();

                    Debug.Assert(keyValue != null);
                    Debug.Assert(valueArray != null);

                    if (Marshal.SizeOf(structType) > valueArray.Length)
                    {
                        throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Struct '{0}' is bigger than the binary metadata for key '{1}'.", structType.FullName, keyValue.Key));
                    }

                    unsafe
                    {
                        fixed(byte *pValue = valueArray)
                        {
                            IntPtr ptr = new IntPtr(pValue);

                            value             = Marshal.PtrToStructure(ptr, structType);
                            this.cachedStruct = value;
                        }
                    }
                }
            }

            return(value);
        }
        public void SetBuffer(int bufferSize, IntPtr buffer, int startIndex)
        {
            if (bufferSize > 0)
            {
                if (buffer == IntPtr.Zero)
                {
                    throw new ArgumentNullException("buffer");
                }

                MetadataKeyValuePair keyValue = this.KeyValue;
                byte[] valueArray             = this.GetValueArray();

                Debug.Assert(keyValue != null);
                Debug.Assert(valueArray != null);

                Marshal.Copy(buffer, valueArray, startIndex, bufferSize);

                Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                {
                    keyValue.Value = valueArray;
                }));
            }
        }
 public BinaryMetadataProxy(MetadataKeyValuePair keyValue, ReadOnlyCollection <byte> data) :
     base(keyValue, data)
 {
     DebugHelper.AssertUIThread();
 }