Beispiel #1
0
        public static PathStruct FromObject(object obj)
        {
            if (obj == null)
                return null;

            Type type = obj.GetType();
            MemberInfo[] memberInfos = type.FindMembers(MemberTypes.Field | MemberTypes.Property, BindingFlags.Public, null, null);

            int sz = memberInfos.Length;
            if (sz == 0)
                return new PathStruct();

            PathStruct pathStruct = new PathStruct();

            for (int i = 0; i < sz; i++) {
                MemberInfo memberInfo = memberInfos[i];

                string memberName = memberInfo.Name;
                object value;
                if (memberInfo is FieldInfo) {
                    value = ((FieldInfo)memberInfo).GetValue(obj);
                } else {
                    value = ((PropertyInfo)memberInfo).GetValue(obj, null);
                }

                pathStruct.SetValue(memberName, new PathValue(value));
            }

            return pathStruct;
        }
Beispiel #2
0
        internal PathValue(object value)
        {
            valueTypeCode = GetTypeCode(value, out valueType);
            if (valueTypeCode == PathValueType.Array)
            {
                array = (Array)value;
            }
            else if (valueTypeCode == PathValueType.Struct)
            {
                value = PathStruct.FromObject(value);
            }

            this.value = value;
        }
Beispiel #3
0
        public static PathStruct FromObject(object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            Type type = obj.GetType();

            MemberInfo[] memberInfos = type.FindMembers(MemberTypes.Field | MemberTypes.Property, BindingFlags.Public, null, null);

            int sz = memberInfos.Length;

            if (sz == 0)
            {
                return(new PathStruct());
            }

            PathStruct pathStruct = new PathStruct();

            for (int i = 0; i < sz; i++)
            {
                MemberInfo memberInfo = memberInfos[i];

                string memberName = memberInfo.Name;
                object value;
                if (memberInfo is FieldInfo)
                {
                    value = ((FieldInfo)memberInfo).GetValue(obj);
                }
                else
                {
                    value = ((PropertyInfo)memberInfo).GetValue(obj, null);
                }

                pathStruct.SetValue(memberName, new PathValue(value));
            }

            return(pathStruct);
        }
        private PathValue ReadValue(PathValueType type)
        {
            if (type == PathValueType.Null)
                return PathValue.Null;

            if (type == PathValueType.Boolean)
                return new PathValue(input.ReadBoolean());

            if (type == PathValueType.Byte)
                return new PathValue(input.ReadByte());
            if (type == PathValueType.Int16)
                return new PathValue(input.ReadInt16());
            if (type == PathValueType.Int32)
                return new PathValue(input.ReadInt32());
            if (type == PathValueType.Int64)
                return new PathValue(input.ReadInt64());
            if (type == PathValueType.Single)
                return new PathValue(input.ReadSingle());
            if (type == PathValueType.Double)
                return new PathValue(input.ReadDouble());

            if (type == PathValueType.String) {
                int length = input.ReadInt32();
                StringBuilder sb = new StringBuilder(length);
                for (int i = 0; i < length; i++) {
                    sb.Append(input.ReadChar());
                }

                return new PathValue(sb.ToString());
            }

            if (type == PathValueType.DateTime)
                return new PathValue(UnixDateTime.ToDateTime(input.ReadInt64()));

            if (type == PathValueType.Struct) {
                int memberCount = input.ReadInt32();
                PathStruct pathStruct = new PathStruct();
                for (int i = 0; i < memberCount; i++) {
                    string memberName = ReadValue(PathValueType.String);
                    PathValue value = ReadValue();
                    pathStruct.SetValue(memberName, value);
                }

                return new PathValue(pathStruct);
            }

            if (type == PathValueType.Array) {
                PathValueType elementType = (PathValueType) input.ReadByte();
                int length = input.ReadInt32();
                PathValue array = PathValue.CreateArray(elementType, length);

                for (int i = 0; i < length; i++) {
                    PathValue value = ReadValue(elementType);
                    array.SetValue(i, value.Value);
                }

                return array;
            }

            throw new NotSupportedException();
        }
Beispiel #5
0
        private void WriteValue(object value)
        {
            if (value == null)
            {
                return;
            }

            if (value is byte)
            {
                output.Write((byte)value);
            }
            else if (value is short)
            {
                output.Write((short)value);
            }
            else if (value is int)
            {
                output.Write((int)value);
            }
            else if (value is long)
            {
                output.Write((long)value);
            }
            else if (value is float)
            {
                output.Write((float)value);
            }
            else if (value is double)
            {
                output.Write((double)value);
            }
            else if (value is DateTime)
            {
                output.Write(UnixDateTime.ToUnixTimestamp((DateTime)value));
            }
            else if (value is String)
            {
                string s  = (string)value;
                int    sz = s.Length;
                output.Write(sz);
                for (int i = 0; i < sz; i++)
                {
                    output.Write(s[i]);
                }
            }
            else if (value is Array)
            {
                //TODO:
            }
            else if (value is PathStruct)
            {
                PathStruct pathStruct  = (PathStruct)value;
                string[]   memberNames = pathStruct.MemberNames;
                int        sz          = pathStruct.MemberCount;

                output.Write(sz);
                for (int i = 0; i < sz; i++)
                {
                    string memberName = memberNames[i];
                    WriteValue(memberName);
                    Write(pathStruct.GetValue(memberName));
                }
            }
            else
            {
                throw new ArgumentException("The value is not supported.");
            }
        }
Beispiel #6
0
 public void Write(PathStruct value)
 {
     Write(new PathValue(value));
 }
 public void Write(PathStruct value)
 {
     Write(new PathValue(value));
 }
Beispiel #8
0
        private PathValue ReadValue(PathValueType type)
        {
            if (type == PathValueType.Null)
            {
                return(PathValue.Null);
            }

            if (type == PathValueType.Boolean)
            {
                return(new PathValue(input.ReadBoolean()));
            }

            if (type == PathValueType.Byte)
            {
                return(new PathValue(input.ReadByte()));
            }
            if (type == PathValueType.Int16)
            {
                return(new PathValue(input.ReadInt16()));
            }
            if (type == PathValueType.Int32)
            {
                return(new PathValue(input.ReadInt32()));
            }
            if (type == PathValueType.Int64)
            {
                return(new PathValue(input.ReadInt64()));
            }
            if (type == PathValueType.Single)
            {
                return(new PathValue(input.ReadSingle()));
            }
            if (type == PathValueType.Double)
            {
                return(new PathValue(input.ReadDouble()));
            }

            if (type == PathValueType.String)
            {
                int           length = input.ReadInt32();
                StringBuilder sb     = new StringBuilder(length);
                for (int i = 0; i < length; i++)
                {
                    sb.Append(input.ReadChar());
                }

                return(new PathValue(sb.ToString()));
            }

            if (type == PathValueType.DateTime)
            {
                return(new PathValue(UnixDateTime.ToDateTime(input.ReadInt64())));
            }

            if (type == PathValueType.Struct)
            {
                int        memberCount = input.ReadInt32();
                PathStruct pathStruct  = new PathStruct();
                for (int i = 0; i < memberCount; i++)
                {
                    string    memberName = ReadValue(PathValueType.String);
                    PathValue value      = ReadValue();
                    pathStruct.SetValue(memberName, value);
                }

                return(new PathValue(pathStruct));
            }

            if (type == PathValueType.Array)
            {
                PathValueType elementType = (PathValueType)input.ReadByte();
                int           length      = input.ReadInt32();
                PathValue     array       = PathValue.CreateArray(elementType, length);

                for (int i = 0; i < length; i++)
                {
                    PathValue value = ReadValue(elementType);
                    array.SetValue(i, value.Value);
                }

                return(array);
            }

            throw new NotSupportedException();
        }