Пример #1
0
        public static Func <byte[], object> GetBytesToValue(Type type)
        {
            if (type == typeof(int))
            {
                return(ValueConverter.FromIntBytes);
            }
            else if (type == typeof(string))
            {
                return(ValueConverter.FromStringBytes);
            }
            else if (type == typeof(bool))
            {
                return(ValueConverter.FromBoolBytes);
            }
            else if (type == typeof(DateTime))
            {
                return(ValueConverter.FromDateTimeBytes);
            }
            else if (type == typeof(Guid))
            {
                return(ValueConverter.FromGuidBytes);
            }
            else if (type == typeof(byte))
            {
                return(ValueConverter.FromByteBytes);
            }
            else if (type == typeof(byte[]))
            {
                return(ValueConverter.FromByteArrayBytes);
            }
            else if (type == typeof(decimal))
            {
                return(ValueConverter.FromDecimalBytes);
            }
            else if (type == typeof(double))
            {
                return(ValueConverter.FromDoubleBytes);
            }
            else if (type == typeof(float))
            {
                return(ValueConverter.FromFloatBytes);
            }
            else if (type == typeof(Int16))
            {
                return(ValueConverter.FromShortBytes);
            }
            else if (type == typeof(Int64))
            {
                return(ValueConverter.FromLongBytes);
            }
            else if (type == typeof(System.Net.IPAddress))
            {
                return(ValueConverter.FromBytesToIpaddress);
            }
            else if (type == typeof(object))
            {
                return(ValueConverter.FromObjectBytes);
            }

            else if (ObjectHelper.IsDictionary(type))
            {
                return(new DictionaryConverter(type).FromBytes);
            }
            else if (ObjectHelper.IsList(type))
            {
                return(new ListConverter(type).FromBytes);
            }
            else if (ObjectHelper.IsCollection(type))
            {
                return(new CollectionConverter(type).FromBytes);
            }

            else if (type.IsClass)
            {
                ClassConverter converter = ClassConverterCache.Get(type);
                if (converter == null)
                {
                    converter = new ClassConverter(type);
                    ClassConverterCache.Add(type, converter);
                    converter.InitFields();
                }

                return(converter.FromBytes);
            }
            return(null);
        }
Пример #2
0
        public static Func <object, byte[]> GetValueToBytes(Type type)
        {
            if (type == typeof(int))
            {
                return(ValueConverter.IntToBytes);
            }
            else if (type == typeof(string))
            {
                return(ValueConverter.StringToBytes);
            }
            else if (type == typeof(bool))
            {
                return(ValueConverter.BoolToBytes);
            }
            else if (type == typeof(DateTime))
            {
                return(ValueConverter.DateTimeToBytes);
            }
            else if (type == typeof(Guid))
            {
                return(ValueConverter.GuidToBytes);
            }
            else if (type == typeof(byte))
            {
                return(ValueConverter.ByteToBytes);
            }
            else if (type == typeof(byte[]))
            {
                return(ValueConverter.ByteArrayToBytes);
            }
            else if (type == typeof(decimal))
            {
                return(ValueConverter.DecimalToBytes);
            }
            else if (type == typeof(double))
            {
                return(ValueConverter.DoubleToBytes);
            }
            else if (type == typeof(float))
            {
                return(ValueConverter.FloatToBytes);
            }
            else if (type == typeof(Int16))
            {
                return(ValueConverter.ShortToBytes);
            }
            else if (type == typeof(Int64))
            {
                return(ValueConverter.LongToBytes);
            }
            else if (type == typeof(System.Net.IPAddress))
            {
                return(ValueConverter.IpAddressToBytes);
            }
            else if (type == typeof(object))
            {
                return(ValueConverter.ObjectToTypes);
            }

            else if (ObjectHelper.IsDictionary(type))
            {
                return(new DictionaryConverter(type).ToBytes);
            }
            else if (ObjectHelper.IsList(type))
            {
                return(new ListConverter(type).ToBytes);
            }
            else if (ObjectHelper.IsCollection(type))
            {
                return(new CollectionConverter(type).ToBytes);
            }
            else if (type.IsClass)
            {
                ClassConverter converter = ClassConverterCache.Get(type);
                if (converter == null)
                {
                    converter = new ClassConverter(type);
                    ClassConverterCache.Add(type, converter);
                    converter.InitFields();
                }

                return(converter.ToBytes);
            }

            return(null);
        }