Ejemplo n.º 1
0
 private void SetItemType(IEnumerable list)
 {
     if (ItemType == null & list != null)
     {
         ItemType = MetaDataHelper.GetEnumerableItemType(list);
     }
 }
        ///<summary>
        ///	Gets data from the specified object and serializes it into the outgoing data stream
        ///	This class implements the debugee side of the visualizer. It is responsible for running the commands against the server.
        ///</summary>
        ///<remarks>
        ///	Straegy is: if the items are serializable then
        ///	if the enumerable is also serializable
        ///	serialize the enumerable
        ///	else
        ///	create a ObjectListView to contains the items and serialize that instead.
        ///	Full back is to copy the enumerable to a data table and serialize that instead.
        ///</remarks>
        ///<param name = "target">Object being visualized.</param>
        ///<param name = "outgoingData">Outgoing data stream.</param>
        public override void GetData(object target, Stream outgoingData)
        {
            var enumerable = target as IEnumerable;

            if (enumerable != null)
            {
                var itemType  = MetaDataHelper.GetEnumerableItemType(enumerable);
                var queryable = enumerable as IQueryable;
                if (queryable != null)
                {
                    enumerable = queryable.Take(100);
                }
                var enumerableType = enumerable.GetType();
                if (itemType.IsSerializable)
                {
                    if (enumerableType.IsSerializable)
                    {
                        Serialize(outgoingData, enumerable);
                    }
                    else
                    {
                        try
                        {
                            var bindingListView = enumerable.ToBindingListView();
                            if (!bindingListView.GetType().IsSerializable)                             // If the IBindingListView is not serializable use a ObjectListView
                            {
                                bindingListView = new ObjectListView(bindingListView);
                            }
                            Serialize(outgoingData, bindingListView);
                        }
                        catch (Exception)
                        {
                            Serialize(outgoingData, enumerable.CopyToDataTable());
                        }
                    }
                }
                else
                {
                    Serialize(outgoingData, enumerable.CopyToDataTable());
                }
            }
            else if (target is DataTable)
            {
                Serialize(outgoingData, (DataTable)target);
            }
        }
        public void GetEnumerableItemTypeTest()
        {
            Assert.AreEqual(typeof(int), MetaDataHelper.GetEnumerableItemType(new List <int>()));
            Assert.AreEqual(typeof(int), MetaDataHelper.GetEnumerableItemType((new List <int> {
                1, 2, 3, 4
            }).Where(i => i > 2)));
            Assert.AreEqual(typeof(AddressTypeEntity), MetaDataHelper.GetEnumerableItemType(new AddressTypeCollection()));
            Assert.AreEqual(typeof(AddressTypeEntity), MetaDataHelper.GetEnumerableItemType(MetaSingletons.MetaData.AddressType));
            Assert.AreEqual(typeof(int), MetaDataHelper.GetEnumerableItemType(new ArrayList {
                1, 2, 3
            }));
            Assert.AreEqual(typeof(object), MetaDataHelper.GetEnumerableItemType(new ArrayList()));

            Assert.AreEqual(typeof(string), MetaDataHelper.GetEnumerableItemType(new string[0]));
            var emptySerializableClasses = new SerializableClass[0];

            Assert.AreEqual(typeof(SerializableClass), MetaDataHelper.GetEnumerableItemType(emptySerializableClasses));
            Assert.AreEqual(typeof(SerializableClass), MetaDataHelper.GetEnumerableItemType(emptySerializableClasses.Take(30)));
        }
Ejemplo n.º 4
0
        private static IBindingListView ToObjectListView(this IEnumerable enumerable)
        {
            var itemType = MetaDataHelper.GetEnumerableItemType(enumerable);

            if (itemType == typeof(string))
            {
                return(null);
            }
            enumerable = (IEnumerable)ListBindingHelper.GetList(enumerable);
            if (enumerable is IList)
            {
                var objectListView = ((IList)enumerable).ToObjectListView();
                if (objectListView != null)
                {
                    return(objectListView);
                }
            }
            return(CreateObjectListViewViaBindingSource(enumerable));
        }