示例#1
0
        public static byte[] ObjectToTypes(object value)
        {
            if (value == null)
            {
                return(null);
            }
            Type type = value.GetType();

            var tobytes = ConverterHelper.GetValueToBytes(type);

            if (tobytes == null)
            {
                throw new Exception("sorry, only support Value Type, like Int, guid, datetime, string as the object value now.");
            }
            else
            {
                var  ValueBytes = tobytes(value);
                var  enumtype   = ConverterHelper.GetEnumType(type);
                byte enumbyte   = (byte)enumtype;

                byte[] Result = new byte[ValueBytes.Length + 1];

                Result[0] = enumbyte;
                System.Buffer.BlockCopy(ValueBytes, 0, Result, 1, ValueBytes.Length);

                return(Result);
            }
        }
示例#2
0
        public DictionaryConverter(Type DictionaryType, bool KeyIgnoreCase = false)
        {
            this.IsIgnoreCase = KeyIgnoreCase;

            this.DictionaryType = DictionaryType;
            KeyType             = ObjectHelper.GetDictionaryKeyType(DictionaryType);
            ValueType           = ObjectHelper.GetDictionaryValueType(DictionaryType);

            KeyLength   = ConverterHelper.GetTypeLength(KeyType);
            Valuelength = ConverterHelper.GetTypeLength(ValueType);

            GetKeyObjectBytes = ConverterHelper.GetValueToBytes(KeyType);
            GetKeyObjectValue = ConverterHelper.GetBytesToValue(KeyType);

            GetValueObjectBytes = ConverterHelper.GetValueToBytes(ValueType);
            GetValueObjectValue = ConverterHelper.GetBytesToValue(ValueType);

            if (GetKeyObjectBytes == null || GetKeyObjectValue == null)
            {
                throw new Exception(KeyType.Name + " is not yet supported.");
            }

            if (GetValueObjectBytes == null || GetValueObjectValue == null)
            {
                throw new Exception(ValueType.Name + " is not yet supported.");
            }
        }
示例#3
0
        public byte[] GetValueBytes(Type type, object value)
        {
            var converter = ConverterHelper.GetValueToBytes(type);

            if (converter != null)
            {
                return(converter(value));
            }
            else
            {
                return(new byte[0]);
            }
        }
示例#4
0
        public CollectionConverter(Type CollectionType)
        {
            this.CollectionType = CollectionType;
            this.DataType       = ObjectHelper.GetEnumberableType(CollectionType);

            this.FieldLength = ConverterHelper.GetTypeLength(this.DataType);

            this.GetObjectBytes = ConverterHelper.GetValueToBytes(this.DataType);
            this.GetObjectValue = ConverterHelper.GetBytesToValue(this.DataType);

            if (this.GetObjectBytes == null || this.GetObjectValue == null)
            {
                throw new Exception(this.DataType.Name + " is not yet supported.");
            }
        }