PasswordCommon.cs 2.02 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FLY.Thick.Base.Common
{
    public class PasswordCell:Misc.ISaveToXml
    {
        static PasswordCell()
        {
            Misc.SaveToXmlHepler.Regist(typeof(PasswordCell));
        }
        /// <summary>
        /// 密码
        /// </summary>
        string password;
        public string Password 
        {
            get { return password; }
            set {
                if (password != value) 
                {
                    password = value;
                }
            }
        }
        /// <summary>
        /// 权限, 数值越大,权限越大。
        /// </summary>
         int level;
        public int Level
        {
            get { return level; }
            set
            {
                if (level != value)
                {
                    level = value;
                }
            }
        }
        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();
            byte[] bs = Misc.Converter.StringToBytes(password);

            buf.AddRange(BitConverter.GetBytes(bs.Length));
            buf.AddRange(bs);
            buf.AddRange(BitConverter.GetBytes(level));
            return buf.ToArray();
        }

        public bool TryParse(byte[] value, int index, out int count)
        {
            count = 8;
            if (value.Length-index < count)
                return false;
            int idx = index;
            int len = BitConverter.ToInt32(value, idx);
            idx += 4;
            count += len;
            if (value.Length - index < count)
                return false;
            password = Misc.Converter.BytesToString(value, idx, len);
            idx += len;
            level = BitConverter.ToInt32(value, idx);
            idx += 4;
            return true;
        }

        public string[] GetSavePropertyNames()
        {
            return new string[]{
                "Password",
                "Level"};
            
        }
    }
}