igor-tkachev / bltoolkit

Business Logic Toolkit for .NET
MIT License
297 stars 112 forks source link

Bug in SqlValueBase #362

Closed ghost closed 9 years ago

ghost commented 9 years ago

This code works wrong with byte arrays. It is added in commit "* #344 test (fails for MySql LinqService, MySql changes column type on iteration)" SHA-1: fc4fdf65b7c523361645cd6d521fc9d149f44fd4

I do not know exact purpose of changes in this commit but I guess we should check it for byte arrays in addition:

if (!(_value is string) && !(_value is byte[]) && _value is IEnumerable)

My fork works with byte-arrays as parameters (because many columns have binary(16) datatype). And we do not use Binary class to work with data types like "binary(n)".

public virtual object Value { get { if (_convertedValue != null) return _convertedValue;

            var valueConverter = ValueConverter;
            if (valueConverter == null)
                return _value;

            if (_value == null)
                return _convertedValue = valueConverter(_value);

            if (!(_value is string)
                && _value is IEnumerable)
            {

if SILVERLIGHT

                var res = new List<Object>(10);

else

                var res = new ArrayList(10);

endif

                var e   = ((IEnumerable)_value);
                var ut  = null as Type;

                foreach (var val in e)
                {
                    var element = valueConverter(val);
                    res.Add(element);

                    if (ut == null)
                        ut = element.GetType();
                    else if (ut != element.GetType())
                        ut = typeof(object);
                }

                if (ut == null)
                    ut = typeof (object);

                if (ut != typeof (object))
                {
                    var array = Array.CreateInstance(ut, res.Count);

if !SILVERLIGHT

                    res.CopyTo(array);

else

                    for(var i = 0; i < res.Count; i++)
                        array.SetValue(res[i], i);

endif

                    _convertedValue = array;
                }
                else
                    _convertedValue = res.ToArray();

            }
            else
                _convertedValue = valueConverter(_value);

            return _convertedValue;
        }

        set
        {
            _value = value;
            _convertedValue = null;
        }
    }