Пример #1
0
        public void SetArray(TypeCollection types, object[] array, System.Type managedType = null)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }

            if (managedType == null && array.Length == 0)
            {
                throw new ArgumentNullException("managedType");
            }

            if (managedType == null)
            {
                managedType = array[0].GetType();
            }

            Type type = types.FindTypeByManagedType(managedType);

            if (type == null)
            {
                throw new Exception("Type not found for " + managedType.FullName);
            }

            int elementSize = Marshal.SizeOf(managedType);

            if (array.Length == 0)
            {
                ValueNative.SetArray(m_nativeInstance, IntPtr.Zero, elementSize, 0, type.Crc);
                return;
            }

            IntPtr arrayData = Marshal.AllocHGlobal(elementSize * array.Length);

            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == null)
                {
                    throw new ArgumentNullException("array[" + i.ToString() + "]");
                }

                if (array[i].GetType() != managedType)
                {
                    throw new Exception("Type '" + array[i].GetType().FullName + "' in array[" + i.ToString() + "] doesn't match given Type '" + managedType.FullName + "'");
                }

                Marshal.StructureToPtr(array[i], arrayData, false);
                arrayData += elementSize;
            }

            ValueNative.SetArray(m_nativeInstance, arrayData, elementSize, array.Length, type.Crc);
            Marshal.FreeHGlobal(arrayData);
        }
Пример #2
0
        public void SetStruct(TypeCollection types, object data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            Type type = types.FindTypeByManagedType(data.GetType());

            if (type == null)
            {
                throw new Exception("Type not found for " + data.GetType().FullName);
            }

            int    structSize = Marshal.SizeOf(type.ManagedType);
            IntPtr structData = Marshal.AllocHGlobal(structSize);

            Marshal.StructureToPtr(data, structData, false);
            ValueNative.SetStruct(m_nativeInstance, structData, structSize, type.Crc);

            Marshal.FreeHGlobal(structData);
        }