コード例 #1
0
        /// <summary>
        /// Parses a data object from the current parsing position.
        /// This can either be a NSData object or a GnuStep NSNumber or NSDate.
        /// The prerequisite for calling this method is, that a data begin token has been read.
        /// </summary>
        /// <returns>The data object found at the parsing position.</returns>
        NSObject ParseData()
        {
            NSObject obj = null;

            //Skip begin token
            Skip();
            if (Accept(DATA_GSOBJECT_BEGIN_TOKEN))
            {
                Skip();
                Expect(DATA_GSBOOL_BEGIN_TOKEN, DATA_GSDATE_BEGIN_TOKEN, DATA_GSINT_BEGIN_TOKEN, DATA_GSREAL_BEGIN_TOKEN);
                if (Accept(DATA_GSBOOL_BEGIN_TOKEN))
                {
                    //Boolean
                    Skip();
                    Expect(DATA_GSBOOL_TRUE_TOKEN, DATA_GSBOOL_FALSE_TOKEN);
                    if (Accept(DATA_GSBOOL_TRUE_TOKEN))
                    {
                        obj = new NSNumber(true);
                    }
                    else
                    {
                        obj = new NSNumber(false);
                    }
                    //Skip the parsed boolean token
                    Skip();
                }
                else if (Accept(DATA_GSDATE_BEGIN_TOKEN))
                {
                    //Date
                    Skip();
                    string dateString = ReadInputUntil(DATA_END_TOKEN);
                    obj = new NSDate(dateString);
                }
                else if (Accept(DATA_GSINT_BEGIN_TOKEN, DATA_GSREAL_BEGIN_TOKEN))
                {
                    //Number
                    Skip();
                    string numberString = ReadInputUntil(DATA_END_TOKEN);
                    obj = new NSNumber(numberString);
                }
                //parse data end token
                Read(DATA_END_TOKEN);
            }
            else
            {
                string dataString = ReadInputUntil(DATA_END_TOKEN);
                dataString = Regex.Replace(dataString, "\\s+", "");

                int    numBytes = dataString.Length / 2;
                byte[] bytes    = new byte[numBytes];
                for (int i = 0; i < bytes.Length; i++)
                {
                    string byteString = dataString.Substring(i * 2, 2);
                    int    byteValue  = Convert.ToInt32(byteString, 16);
                    bytes[i] = (byte)byteValue;
                }
                obj = new NSData(bytes);

                //skip end token
                Skip();
            }

            return(obj);
        }
コード例 #2
0
        /// <summary>
        /// Converts this NSObject into an equivalent object
        /// of the .NET Runtime Environment.
        /// <para><see cref="NSArray"/> objects are converted to arrays.</para>
        /// <para><see cref="NSDictionary"/> objects are converted to objects extending the <see cref="Dictionary{TKey, TValue}"/> class.</para>
        /// <para><see cref="NSSet"/> objects are converted to objects extending the <see cref="List{NSObject}"/> class.</para>
        /// <para><see cref="NSNumber"/> objects are converted to primitive number values (<see cref="int"/>, <see cref="long"/>, <see cref="double"/> or <see cref="bool"/>).</para>
        /// <para><see cref="NSString"/> objects are converted to <see cref="string"/> objects.</para>
        /// <para><see cref="NSData"/> objects are converted to <see cref="byte"/> arrays.</para>
        /// <para><see cref="NSDate"/> objects are converted to <see cref="System.DateTime"/> objects.</para>
        /// <para><see cref="UID"/> objects are converted to <see cref="byte"/> arrays.</para>
        /// </summary>
        /// <returns>A native .NET object representing this NSObject's value.</returns>
        public Object ToObject()
        {
            if (this is NSArray)
            {
                NSObject[] arrayA = ((NSArray)this).GetArray();
                Object[]   arrayB = new Object[arrayA.Length];
                for (int i = 0; i < arrayA.Length; i++)
                {
                    arrayB[i] = arrayA[i].ToObject();
                }
                return(arrayB);
            }
            if (this is NSDictionary)
            {
                Dictionary <string, NSObject> dictA = ((NSDictionary)this).GetDictionary();
                Dictionary <string, Object>   dictB = new Dictionary <string, Object>(dictA.Count);
                foreach (KeyValuePair <string, NSObject> kvp in dictA)
                {
                    dictB.Add(kvp.Key, kvp.Value.ToObject());
                }
                return(dictB);
            }
            if (this is NSSet)
            {
                List <NSObject> setA = ((NSSet)this).GetSet();
                List <Object>   setB = new List <Object>();
                foreach (NSObject o in setA)
                {
                    setB.Add(o.ToObject());
                }
                return(setB);
            }
            if (this is NSNumber)
            {
                NSNumber num = (NSNumber)this;
                switch (num.GetNSNumberType())
                {
                case NSNumber.INTEGER: {
                    long longVal = num.ToLong();
                    if (longVal > int.MaxValue || longVal < int.MinValue)
                    {
                        return(longVal);
                    }
                    return(num.ToInt());
                }

                case NSNumber.REAL:
                    return(num.ToDouble());

                case NSNumber.BOOLEAN:
                    return(num.ToBool());

                default:
                    return(num.ToDouble());
                }
            }
            if (this is NSString)
            {
                return(((NSString)this).GetContent());
            }
            if (this is NSData)
            {
                return(((NSData)this).Bytes);
            }
            if (this is NSDate)
            {
                return(((NSDate)this).Date);
            }
            if (this is UID)
            {
                return(((UID)this).Bytes);
            }
            return(this);
        }