ToIntArray() public static method

public static ToIntArray ( int array ) : IntPtr
array int
return System.IntPtr
Esempio n. 1
0
        public static IntPtr ConvertToJNIArray(Array array)
        {
            Type elementType = array.GetType().GetElementType();

            if (AndroidReflection.IsPrimitive(elementType))
            {
                if (elementType == typeof(int))
                {
                    return(AndroidJNISafe.ToIntArray((int[])array));
                }
                if (elementType == typeof(bool))
                {
                    return(AndroidJNISafe.ToBooleanArray((bool[])array));
                }
                if (elementType == typeof(byte))
                {
                    return(AndroidJNISafe.ToByteArray((byte[])array));
                }
                if (elementType == typeof(short))
                {
                    return(AndroidJNISafe.ToShortArray((short[])array));
                }
                if (elementType == typeof(long))
                {
                    return(AndroidJNISafe.ToLongArray((long[])array));
                }
                if (elementType == typeof(float))
                {
                    return(AndroidJNISafe.ToFloatArray((float[])array));
                }
                if (elementType == typeof(double))
                {
                    return(AndroidJNISafe.ToDoubleArray((double[])array));
                }
                if (elementType != typeof(char))
                {
                    return(IntPtr.Zero);
                }
                return(AndroidJNISafe.ToCharArray((char[])array));
            }
            if (elementType == typeof(string))
            {
                string[] strArray = (string[])array;
                int      size     = array.GetLength(0);
                IntPtr   clazz    = AndroidJNISafe.FindClass("java/lang/String");
                IntPtr   ptr2     = AndroidJNI.NewObjectArray(size, clazz, IntPtr.Zero);
                for (int j = 0; j < size; j++)
                {
                    IntPtr ptr3 = AndroidJNISafe.NewStringUTF(strArray[j]);
                    AndroidJNI.SetObjectArrayElement(ptr2, j, ptr3);
                    AndroidJNISafe.DeleteLocalRef(ptr3);
                }
                AndroidJNISafe.DeleteLocalRef(clazz);
                return(ptr2);
            }
            if (elementType != typeof(AndroidJavaObject))
            {
                throw new Exception("JNI; Unknown array type '" + elementType + "'");
            }
            AndroidJavaObject[] objArray = (AndroidJavaObject[])array;
            int length = array.GetLength(0);

            IntPtr[] ptrArray = new IntPtr[length];
            IntPtr   localref = AndroidJNISafe.FindClass("java/lang/Object");
            IntPtr   zero     = IntPtr.Zero;

            for (int i = 0; i < length; i++)
            {
                if (objArray[i] != null)
                {
                    ptrArray[i] = objArray[i].GetRawObject();
                    IntPtr rawClass = objArray[i].GetRawClass();
                    if (zero != rawClass)
                    {
                        if (zero == IntPtr.Zero)
                        {
                            zero = rawClass;
                        }
                        else
                        {
                            zero = localref;
                        }
                    }
                }
                else
                {
                    ptrArray[i] = IntPtr.Zero;
                }
            }
            IntPtr ptr7 = AndroidJNISafe.ToObjectArray(ptrArray, zero);

            AndroidJNISafe.DeleteLocalRef(localref);
            return(ptr7);
        }
Esempio n. 2
0
        public static IntPtr ConvertToJNIArray(Array array)
        {
            Type elementType = array.GetType().GetElementType();

            if (AndroidReflection.IsPrimitive(elementType))
            {
                if (elementType == typeof(int))
                {
                    return(AndroidJNISafe.ToIntArray((int[])array));
                }
                if (elementType == typeof(bool))
                {
                    return(AndroidJNISafe.ToBooleanArray((bool[])array));
                }
                if (elementType == typeof(byte))
                {
                    return(AndroidJNISafe.ToByteArray((byte[])array));
                }
                if (elementType == typeof(short))
                {
                    return(AndroidJNISafe.ToShortArray((short[])array));
                }
                if (elementType == typeof(long))
                {
                    return(AndroidJNISafe.ToLongArray((long[])array));
                }
                if (elementType == typeof(float))
                {
                    return(AndroidJNISafe.ToFloatArray((float[])array));
                }
                if (elementType == typeof(double))
                {
                    return(AndroidJNISafe.ToDoubleArray((double[])array));
                }
                if (elementType == typeof(char))
                {
                    return(AndroidJNISafe.ToCharArray((char[])array));
                }
                return(IntPtr.Zero);
            }
            if (elementType == typeof(string))
            {
                string[] array2  = (string[])array;
                int      length  = array.GetLength(0);
                IntPtr   intPtr  = AndroidJNISafe.FindClass("java/lang/String");
                IntPtr   intPtr2 = AndroidJNI.NewObjectArray(length, intPtr, IntPtr.Zero);
                for (int i = 0; i < length; i++)
                {
                    IntPtr intPtr3 = AndroidJNISafe.NewStringUTF(array2[i]);
                    AndroidJNI.SetObjectArrayElement(intPtr2, i, intPtr3);
                    AndroidJNISafe.DeleteLocalRef(intPtr3);
                }
                AndroidJNISafe.DeleteLocalRef(intPtr);
                return(intPtr2);
            }
            if (elementType == typeof(AndroidJavaObject))
            {
                AndroidJavaObject[] array3 = (AndroidJavaObject[])array;
                int      length2           = array.GetLength(0);
                IntPtr[] array4            = new IntPtr[length2];
                IntPtr   intPtr4           = AndroidJNISafe.FindClass("java/lang/Object");
                IntPtr   intPtr5           = IntPtr.Zero;
                for (int j = 0; j < length2; j++)
                {
                    if (array3[j] != null)
                    {
                        array4[j] = array3[j].GetRawObject();
                        IntPtr rawClass = array3[j].GetRawClass();
                        if (intPtr5 != rawClass)
                        {
                            intPtr5 = ((!(intPtr5 == IntPtr.Zero)) ? intPtr4 : rawClass);
                        }
                    }
                    else
                    {
                        array4[j] = IntPtr.Zero;
                    }
                }
                IntPtr result = AndroidJNISafe.ToObjectArray(array4, intPtr5);
                AndroidJNISafe.DeleteLocalRef(intPtr4);
                return(result);
            }
            throw new Exception("JNI; Unknown array type '" + elementType + "'");
        }
        public static IntPtr ConvertToJNIArray(Array array)
        {
            System.Type elementType = array.GetType().GetElementType();
            if (AndroidReflection.IsPrimitive(elementType))
            {
                if (elementType == typeof(int))
                {
                    return(AndroidJNISafe.ToIntArray((int[])array));
                }
                if (elementType == typeof(bool))
                {
                    return(AndroidJNISafe.ToBooleanArray((bool[])array));
                }
                if (elementType == typeof(byte))
                {
                    return(AndroidJNISafe.ToByteArray((byte[])array));
                }
                if (elementType == typeof(short))
                {
                    return(AndroidJNISafe.ToShortArray((short[])array));
                }
                if (elementType == typeof(long))
                {
                    return(AndroidJNISafe.ToLongArray((long[])array));
                }
                if (elementType == typeof(float))
                {
                    return(AndroidJNISafe.ToFloatArray((float[])array));
                }
                if (elementType == typeof(double))
                {
                    return(AndroidJNISafe.ToDoubleArray((double[])array));
                }
                if (elementType == typeof(char))
                {
                    return(AndroidJNISafe.ToCharArray((char[])array));
                }
                return(IntPtr.Zero);
            }
            if (elementType == typeof(string))
            {
                string[] strArray = (string[])array;
                int      length   = array.GetLength(0);
                IntPtr   num      = AndroidJNISafe.FindClass("java/lang/String");
                IntPtr   array1   = AndroidJNI.NewObjectArray(length, num, IntPtr.Zero);
                for (int index = 0; index < length; ++index)
                {
                    IntPtr localref = AndroidJNISafe.NewStringUTF(strArray[index]);
                    AndroidJNI.SetObjectArrayElement(array1, index, localref);
                    AndroidJNISafe.DeleteLocalRef(localref);
                }
                AndroidJNISafe.DeleteLocalRef(num);
                return(array1);
            }
            if (elementType != typeof(AndroidJavaObject))
            {
                throw new Exception("JNI; Unknown array type '" + (object)elementType + "'");
            }
            AndroidJavaObject[] androidJavaObjectArray = (AndroidJavaObject[])array;
            int length1 = array.GetLength(0);

            IntPtr[] array2    = new IntPtr[length1];
            IntPtr   localref1 = AndroidJNISafe.FindClass("java/lang/Object");
            IntPtr   type      = IntPtr.Zero;

            for (int index = 0; index < length1; ++index)
            {
                if (androidJavaObjectArray[index] != null)
                {
                    array2[index] = androidJavaObjectArray[index].GetRawObject();
                    IntPtr rawClass = androidJavaObjectArray[index].GetRawClass();
                    if (type != rawClass)
                    {
                        type = !(type == IntPtr.Zero) ? localref1 : rawClass;
                    }
                }
                else
                {
                    array2[index] = IntPtr.Zero;
                }
            }
            IntPtr objectArray = AndroidJNISafe.ToObjectArray(array2, type);

            AndroidJNISafe.DeleteLocalRef(localref1);
            return(objectArray);
        }