コード例 #1
0
        // ReSharper disable once InconsistentNaming
        public static NSObject?ToNSObject(object?obj)
        {
            if (obj == null)
            {
                return(null);
            }

            var type = obj.GetType();

            if (type.IsPrimitive || type == typeof(string) || type == typeof(byte[]))
            {
                // NSObject can deal with this itself
                return(NSObject.Wrap(obj));
            }

            if (IsSet(type))
            {
                var nsSet = new NSSet();
                foreach (var value in (IEnumerable)obj)
                {
                    nsSet.AddObject(ToNSObject(value));
                }
                return(nsSet);
            }

            if (IsDictionary(type))
            {
                var nsDictionary = new NSDictionary();
                foreach (DictionaryEntry kvp in (IDictionary)obj)
                {
                    nsDictionary.Add((string)kvp.Key, ToNSObject(kvp.Value));
                }
                return(nsDictionary);
            }

            if (IsEnumerable(type))
            {
                var nsArray = new NSArray();
                foreach (var value in (IEnumerable)obj)
                {
                    nsArray.Add(ToNSObject(value));
                }
                return(nsArray);
            }

            var dict = new NSDictionary();

            foreach (var property in type.GetProperties(PropertyFlags))
            {
                var name           = property.Name;
                var dataMemberAttr = property.GetCustomAttribute <DataMemberAttribute>();
                if (dataMemberAttr?.Name != null)
                {
                    name = dataMemberAttr.Name;
                }

                dict.Add(name, ToNSObject(property.GetValue(obj)));
            }
            return(dict);
        }
コード例 #2
0
        public static void TestWrap()
        {
            bool     bl   = true;
            byte     byt  = 24;
            short    shrt = 12;
            int      i    = 42;
            long     lng  = 30000000000L;
            float    flt  = 124.3f;
            double   dbl  = 32.0;
            DateTime date = new DateTime();
            string   strg = "Hello World";

            byte[]        bytes  = new byte[] { (byte)0x00, (byte)0xAF, (byte)0xAF };
            Object[]      array  = new Object[] { bl, byt, shrt, i, lng, flt, dbl, date, strg, bytes };
            int[]         array2 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 3000 };
            List <Object> list   = new List <Object>(array);

            Dictionary <string, Object> map = new Dictionary <string, Object>();

            map.Add("int", i);
            map.Add("long", lng);
            map.Add("date", date);

            NSObject WrappedO = NSObject.Wrap((Object)bl);

            Assert.True(WrappedO.GetType().Equals(typeof(NSNumber)));
            Assert.True(WrappedO.ToObject().Equals(bl));

            WrappedO = NSObject.Wrap((Object)byt);
            Assert.True(WrappedO.GetType().Equals(typeof(NSNumber)));
            Assert.True((int)WrappedO.ToObject() == byt);

            WrappedO = NSObject.Wrap((Object)shrt);
            Assert.True(WrappedO.GetType().Equals(typeof(NSNumber)));
            Assert.True((int)WrappedO.ToObject() == shrt);

            WrappedO = NSObject.Wrap((Object)i);
            Assert.True(WrappedO.GetType().Equals(typeof(NSNumber)));
            Assert.True((int)WrappedO.ToObject() == i);

            WrappedO = NSObject.Wrap((Object)lng);
            Assert.True(WrappedO.GetType().Equals(typeof(NSNumber)));
            Assert.True((long)WrappedO.ToObject() == lng);

            WrappedO = NSObject.Wrap((Object)flt);
            Assert.True(WrappedO.GetType().Equals(typeof(NSNumber)));
            Assert.True((double)WrappedO.ToObject() == flt);

            WrappedO = NSObject.Wrap((Object)dbl);
            Assert.True(WrappedO.GetType().Equals(typeof(NSNumber)));
            Assert.True((double)WrappedO.ToObject() == dbl);

            WrappedO = NSObject.Wrap((Object)date);
            Assert.True(WrappedO.GetType().Equals(typeof(NSDate)));
            Assert.True(((DateTime)WrappedO.ToObject()).Equals(date));

            WrappedO = NSObject.Wrap((Object)strg);
            Assert.True(WrappedO.GetType().Equals(typeof(NSString)));
            Assert.True(((string)WrappedO.ToObject()).Equals(strg));

            WrappedO = NSObject.Wrap((Object)bytes);
            Assert.True(WrappedO.GetType().Equals(typeof(NSData)));
            byte[] data = (byte[])WrappedO.ToObject();
            Assert.True(data.Length == bytes.Length);
            for (int x = 0; x < bytes.Length; x++)
            {
                Assert.True(data[x] == bytes[x]);
            }

            WrappedO = NSObject.Wrap((Object)array);
            Assert.True(WrappedO.GetType().Equals(typeof(NSArray)));
            Object[] objArray = (Object[])WrappedO.ToObject();
            Assert.True(objArray.Length == array.Length);

            WrappedO = NSObject.Wrap((Object)array2);
            Assert.True(WrappedO.GetType().Equals(typeof(NSArray)));
            Assert.True(((NSArray)WrappedO).Count == array2.Length);

            WrappedO = NSObject.Wrap((Object)list);
            Assert.True(WrappedO.GetType().Equals(typeof(NSArray)));
            objArray = (Object[])WrappedO.ToObject();
            Assert.True(objArray.Length == array.Length);

            WrappedO = NSObject.Wrap((Object)map);
            Assert.True(WrappedO.GetType().Equals(typeof(NSDictionary)));
            NSDictionary dict = (NSDictionary)WrappedO;

            Assert.True(((NSNumber)dict.ObjectForKey("int")).ToLong() == i);
            Assert.True(((NSNumber)dict.ObjectForKey("long")).ToLong() == lng);
            Assert.True(((NSDate)dict.ObjectForKey("date")).Date.Equals(date));

            // TODO

            /*
             * Object unWrappedO = WrappedO.ToObject();
             * Map map2 = (Map)unWrappedO;
             * Assert.True(((int)map.get("int")) == i);
             * Assert.True(((long)map.get("long")) == lng);
             * Assert.True(((DateTime)map.get("date")).Equals(date));*/
        }