Beispiel #1
0
        /// <summary>
        /// Function to read a generic value from the stream.
        /// </summary>
        /// <typeparam name="T">Type of value to read.  Must be a value type.</typeparam>
        /// <returns>The value in the stream.</returns>
        public unsafe T ReadValue <T>()
            where T : struct
        {
            T     returnVal;
            int   size    = DirectAccess.SizeOf <T>();
            byte *pointer = stackalloc byte[size];
            byte *bytes   = pointer;

            while (size > 0)
            {
                if (size >= 8)
                {
                    *((long *)bytes) = ReadInt64();
                    bytes           += 8;
                    size            -= 8;
                }
                else if (size >= 4)
                {
                    *((int *)bytes) = ReadInt32();
                    bytes          += 4;
                    size           -= 4;
                }
                else if (size >= 2)
                {
                    *((short *)bytes) = ReadInt16();
                    bytes            += 2;
                    size -= 2;
                }
                else
                {
                    *bytes = ReadByte();
                    bytes++;
                    size--;
                }
            }

            DirectAccess.ReadValue(pointer, out returnVal);

            return(returnVal);
        }
 /// <summary>
 /// Function to read a specific value from the memory pointed at by the pointer.
 /// </summary>
 /// <typeparam name="T">Type of value to read.</typeparam>
 /// <param name="source">The source pointer.</param>
 /// <param name="value">The value that was read from memory.</param>
 /// <returns>The value at the pointer.</returns>
 /// <remarks>This method can only write value types composed of primitives, reference objects will not work.
 /// <para>There is no way to determine the size of the data pointed at by the pointer, so the user must take care not to write outside the bounds of the memory.</para>
 /// </remarks>
 public static void Read <T>(this IntPtr source, out T value)
     where T : struct
 {
     DirectAccess.ReadValue(source, out value);
 }