Ejemplo n.º 1
0
        internal static TypeEnum GetElementTypeEnum()
        {
            var ty = typeof(T);

            if (ty.IsArray)
            {
                var elTy       = ty.GetElementType();
                var elTypeEnum = VariantHelper.GetTypeEnum(elTy);
                return(elTypeEnum);
            }
            return(TypeEnum.None);
        }
Ejemplo n.º 2
0
 internal static TypeEnum GetElementTypeEnum()
 {
     if (_elementType != -1)
     {
         return (TypeEnum)(byte)_elementType;
     }
     var ty = typeof(T);
     if (ty.IsArray)
     {
         var elTy = ty.GetElementType();
         var elTypeEnum = VariantHelper.GetTypeEnum(elTy);
         _elementType = (int)elTypeEnum;
         return elTypeEnum;
     }
     return TypeEnum.None;
 }
Ejemplo n.º 3
0
        internal static TypeEnum GetElementTypeEnum()
        {
            var ty = typeof(T);

            if (ty.IsArray)
            {
                var elTy       = ty.GetElementType();
                var elTypeEnum = VariantHelper.GetTypeEnum(elTy);
                return(elTypeEnum);
            }

            if (typeof(T).GetTypeInfo().IsGenericType&&
                typeof(T).GetGenericTypeDefinition() == typeof(VectorStorage <>))
            {
                var elTy       = typeof(T).GenericTypeArguments[0];
                var elTypeEnum = VariantHelper.GetTypeEnum(elTy);
                return(elTypeEnum);
            }

            return(TypeEnum.None);
        }
Ejemplo n.º 4
0
        public TypeFlattenner(Type type)
        {
            _accessor = TypeAccessor.Create(type);

            var typeEnum = VariantHelper.GetTypeEnum(type);

            if ((int)typeEnum <= Variant.KnownSmallTypesLimit)
            {
                _layout = Layout.Scalar;
            }

            _members = _accessor.GetMembers();
            for (int i = 0; i < _members.Count; i++)
            {
                if (_members[i].IsDefined(typeof(DataMemberAttribute)))
                {
                    _layout         = Layout.DataContract;
                    _orderedMembers = _orderedMembers ?? new List <Member>();
                    _orderedMembers.Add(_members[i]);
                    var memberTypeEnum = VariantHelper.GetTypeEnum(_members[i].Type);
                    if ((int)memberTypeEnum >= Variant.KnownSmallTypesLimit &&
                        memberTypeEnum != TypeEnum.String)
                    {
                        throw new NotSupportedException("Only scalar data members are supported");
                    }
                }
            }
            if (_layout == Layout.Scalar)
            {
                _width = 1;
                _columns.Add(type.Name);
            }
            else if (_layout == Layout.PublicProperties)
            {
                _width          = _members.Count;
                _orderedMembers = _members.OrderBy(m => m.Name).ToList();
                foreach (var m in _orderedMembers)
                {
                    _columns.Add(m.Name);
                }
            }
            else if (_layout == Layout.DataContract)
            {
                int getorder(Member m)
                {
                    var attribute = ((DataMemberAttribute)m.GetAttribute(typeof(DataMemberAttribute), true));
                    var order     = attribute.Order;

                    if (order <= 0)
                    {
                        order = int.MaxValue;
                    }
                    return(order);
                };
                string getName(Member m)
                {
                    var name = ((DataMemberAttribute)m.GetAttribute(typeof(DataMemberAttribute), true)).Name;

                    if (string.IsNullOrEmpty(name))
                    {
                        name = m.Name;
                    }
                    return(name);
                };

                _orderedMembers = _orderedMembers
                                  .OrderBy(getorder)
                                  .ThenBy(getName)
                                  .ToList();
                _width = _orderedMembers.Count;

                foreach (var m in _orderedMembers)
                {
                    _columns.Add(getName(m));
                }
            }
        }
Ejemplo n.º 5
0
        public static Variant FromObject(object value)
        {
            if (value == null)
            {
                return(new Variant {
                    _header = { TypeEnum = TypeEnum.None }
                });
            }
            // unwrap potentially boxed known types
            var objTypeEnum = VariantHelper.GetTypeEnum(value.GetType());

            if ((int)objTypeEnum < KnownSmallTypesLimit)
            {
                switch (objTypeEnum)
                {
                case TypeEnum.None:
                    throw new InvalidOperationException("TypeEnum.None is possible only for nulls");

                case TypeEnum.Int8:
                    return(Create((sbyte)value));

                case TypeEnum.Int16:
                    return(Create((short)value));

                case TypeEnum.Int32:
                    return(Create((int)value));

                case TypeEnum.Int64:
                    return(Create((long)value));

                case TypeEnum.UInt8:
                    return(Create((byte)value));

                case TypeEnum.UInt16:
                    return(Create((ushort)value));

                case TypeEnum.UInt32:
                    return(Create((uint)value));

                case TypeEnum.UInt64:
                    return(Create((ulong)value));

                case TypeEnum.Float32:
                    return(Create((float)value));

                case TypeEnum.Float64:
                    return(Create((double)value));

                case TypeEnum.Decimal:
                    return(Create((decimal)value));

                case TypeEnum.Price:
                    return(Create((Price)value));

                case TypeEnum.Money:
                    throw new NotImplementedException();
                //return Create((Money)value);

                case TypeEnum.DateTime:
                    return(Create((DateTime)value));

                case TypeEnum.Timestamp:
                    return(Create((Timestamp)value));

                case TypeEnum.Date:
                    throw new NotImplementedException();

                case TypeEnum.Time:
                    throw new NotImplementedException();

                case TypeEnum.Complex32:
                    throw new NotImplementedException();

                case TypeEnum.Complex64:
                    throw new NotImplementedException();

                case TypeEnum.Bool:
                    return(Create((bool)value));

                case TypeEnum.ErrorCode:
                    return(Create((ErrorCode)value));

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            if (objTypeEnum == TypeEnum.Array || objTypeEnum == TypeEnum.String)
            {
                return(Create(value));
                //Environment.FailFast("Array shoud have been dispatched via dynamic in the untyped Create method");
            }

            if (objTypeEnum == TypeEnum.Matrix)
            {
                var elTy        = value.GetType().GetElementType();
                var subTypeEnum = VariantHelper.GetTypeEnum(elTy);
                var v           = new Variant
                {
                    _object = value,
                    _header = new VariantHeader
                    {
                        TypeEnum        = TypeEnum.Matrix,
                        ElementTypeEnum = subTypeEnum
                    }
                };
                return(v);
            }

            if (objTypeEnum == TypeEnum.Table)
            {
                var subTypeEnum = TypeEnum.Variant;
                var v           = new Variant
                {
                    _object = value,
                    _header = new VariantHeader
                    {
                        TypeEnum        = TypeEnum.Matrix,
                        ElementTypeEnum = subTypeEnum
                    }
                };
                return(v);
            }

            if (objTypeEnum == TypeEnum.Object)
            {
                var subTypeEnum = KnownTypeAttribute.GetTypeId(value.GetType());
                var v           = new Variant
                {
                    _object = value,
                    _header = new VariantHeader
                    {
                        TypeEnum        = TypeEnum.Object,
                        ElementTypeEnum = (TypeEnum)subTypeEnum
                    }
                };
                return(v);
            }

            throw new NotImplementedException();
        }