Пример #1
0
        public static IntPtr CFTypeFromManagedType(object objVal)
        {
            if (objVal == null)
            {
                return(IntPtr.Zero);
            }
            IntPtr zero = IntPtr.Zero;
            Type   type = objVal.GetType();

            try
            {
                if (type == typeof(string))
                {
                    return(StringToCFString((string)objVal));
                }
                if ((((type == typeof(short)) || (type == typeof(ushort))) || ((type == typeof(int)) || (type == typeof(uint)))) || (((type == typeof(long)) || (type == typeof(double))) || (type == typeof(float))))
                {
                    return(CFNumberCreateNumbers(objVal));
                }
                if (type == typeof(bool))
                {
                    return(CFBoolean.GetCFBoolean(Convert.ToBoolean(objVal)));
                }
                if (type == typeof(DateTime))
                {
                    string firstDate  = new DateTime(0x7d1, 1, 1, 0, 0, 0, 0).ToString();
                    string secondDate = Convert.ToString(objVal);
                    double diff       = DateAndTime.DateDiff("s", firstDate, secondDate);
                    return(CFDateCreate(kCFAllocatorDefault, diff));
                }
                if (type == typeof(byte[]))
                {
                    byte[] arr     = objVal as byte[];
                    IntPtr ptrDest = Marshal.UnsafeAddrOfPinnedArrayElement(arr, 0);
                    zero = CFDataCreateMutable(kCFAllocatorDefault, (uint)arr.Length);
                    CFDataAppendBytes(zero, ptrDest, (uint)arr.Length);
                    return(zero);
                }
                if (type == typeof(object[]))
                {
                    return(CFArrayFromManageArray((object[])objVal));
                }
                if (type == typeof(ArrayList))
                {
                    return(CFArrayFromManageArrayList(objVal as ArrayList));
                }
                if (type == typeof(List <object>))
                {
                    return(CFArrayFromManageList(objVal as List <object>));
                }
                if (type == typeof(Dictionary <object, object>))
                {
                    zero = CFDictionaryFromManagedDictionary(objVal as Dictionary <object, object>);
                }
            }
            catch
            {
            }
            return(zero);
        }
Пример #2
0
 static CoreFoundation()
 {
     kCFStreamPropertyDataWritten    = EnumToCFEnum("kCFStreamPropertyDataWritten");
     kCFTypeDictionaryKeyCallBacks   = ConstToCFConst("kCFTypeDictionaryKeyCallBacks");
     kCFTypeDictionaryValueCallBacks = ConstToCFConst("kCFTypeDictionaryValueCallBacks");
     kCFTypeArrayCallBacks           = ConstToCFConst("kCFTypeArrayCallBacks");
     kCFBooleanFalse = CFBoolean.GetCFBoolean(false);
     kCFBooleanTrue  = CFBoolean.GetCFBoolean(true);
 }