将对象类型转换为C类

时间:2014-09-13 17:45:31

标签: c#

问题:我试图将对象类型强制转换回根类
我从一个通用的双向链表开始,我能够添加值但不能检索它们

class CarPart
{
    public long PartNumber;
    public string PartName;
    public double UnitPrice;
}

class DoublyLinkedList
{
    private class DoublyLinkedListNode
    {
        private object element;
        private DoublyLinkedListNode next;
        private DoublyLinkedListNode previous;

        public DoublyLinkedListNode(object element)
        {
            this.element = element;
            this.next = null;
            this.previous = null;
        }

        public DoublyLinkedListNode(object element, DoublyLinkedListNode prevNode)
        {
            this.element = element;
            this.previous = prevNode;
            prevNode.next = this;
        }

        public object Element
        {
            get { return this.element; }
            set { this.element = value; }
        }

        public DoublyLinkedListNode Next
        {
            get { return this.next; }
            set { this.next = value; }
        }

        public DoublyLinkedListNode Previous
        {
            get { return this.previous; }
            set { this.previous = value; }
        }
    }

    private DoublyLinkedListNode head;
    private DoublyLinkedListNode tail;
    private int count;

    public DoublyLinkedList()
    {
        this.head = null;
        this.tail = null;
        this.count = 0;
    }

    public int Count
    {
        get { return this.count; }
    }

    public object this[int index]
    {
        get
        {
            if (index >= count || index < 0)
            {
                throw new ArgumentOutOfRangeException("Out of range!");
            }
            DoublyLinkedListNode currentNode = this.head;
            for (int i = 0; i < index; i++)
            {
                currentNode = currentNode.Next;
            }
            return currentNode.Element;
        }
        set
        {
            if (index >= count || index < 0)
            {
                throw new ArgumentOutOfRangeException("Out of range!");
            }
            DoublyLinkedListNode currentNode = this.head;
            for (int i = 0; i < index; i++)
            {
                currentNode = currentNode.Next;
            }
            currentNode.Element = value;
        }
    }
    public void Add(object item)
    {
        if (this.head == null)
        {
            this.head = new DoublyLinkedListNode(item);
            this.tail = this.head;
        }
        else
        {
            DoublyLinkedListNode newItem = new DoublyLinkedListNode(item, tail);
            this.tail = newItem;
        }
        count++;
    }

    public void Insert(object item, int index)
    {
        count++;
        if (index >= count || index < 0)
        {
            throw new ArgumentOutOfRangeException("Out of range!");
        }
        DoublyLinkedListNode newItem = new DoublyLinkedListNode(item);
        int currentIndex = 0;
        DoublyLinkedListNode currentItem = this.head;
        DoublyLinkedListNode prevItem = null;
        while (currentIndex < index)
        {
            prevItem = currentItem;
            currentItem = currentItem.Next;
            currentIndex++;
        }
        if (index == 0)
        {
            newItem.Previous = this.head.Previous;
            newItem.Next = this.head;
            this.head.Previous = newItem;
            this.head = newItem;
        }
        else if (index == count - 1)
        {
            newItem.Previous = this.tail;
            this.tail.Next = newItem;
            newItem = this.tail;
        }
        else
        {
            newItem.Next = prevItem.Next;
            prevItem.Next = newItem;
            newItem.Previous = currentItem.Previous;
            currentItem.Previous = newItem;
        }
    }
    public void Remove(object item)
    {
        int currentIndex = 0;
        DoublyLinkedListNode currentItem = this.head;
        DoublyLinkedListNode prevItem = null;
        while (currentItem != null)
        {
            if ((currentItem.Element != null &&
                currentItem.Element.Equals(item)) ||
                (currentItem.Element == null) && (item == null))
            {
                break;
            }
            prevItem = currentItem;
            currentItem = currentItem.Next;
            currentIndex++;
        }
        if (currentItem != null)
        {
            count--;
            if (count == 0)
            {
                this.head = null;
            }
            else if (prevItem == null)
            {
                this.head = currentItem.Next;
                this.head.Previous = null;
            }
            else if (currentItem == tail)
            {
                currentItem.Previous.Next = null;
                this.tail = currentItem.Previous;
            }
            else
            {
                currentItem.Previous.Next = currentItem.Next;
                currentItem.Next.Previous = currentItem.Previous;
            }
        }
    }
    public void RemoveAt(int index)
    {
        if (index >= this.count || index < 0)
        {
            throw new ArgumentOutOfRangeException("Out of range!");
        }

        int currentIndex = 0;
        DoublyLinkedListNode currentItem = this.head;
        DoublyLinkedListNode prevItem = null;
        while (currentIndex < index)
        {
            prevItem = currentItem;
            currentItem = currentItem.Next;
            currentIndex++;
        }
        if (this.count == 0)
        {
            this.head = null;
        }
        else if (prevItem == null)
        {
            this.head = currentItem.Next;
            this.head.Previous = null;
        }
        else if (index == count - 1)
        {
            prevItem.Next = currentItem.Next;
            tail = prevItem;
            currentItem = null;
        }
        else
        {
            prevItem.Next = currentItem.Next;
            currentItem.Next.Previous = prevItem;
        }
        count--;
    }
    public int indexOf(object item)
    {
        int index = 0;
        DoublyLinkedListNode currentItem = this.head;
        while (currentItem != null)
        {
            if (((currentItem.Element != null) && (item == currentItem.Element)) ||
                ((currentItem.Element == null) && (item == null)))
            {
                return index;
            }
            index++;
            currentItem = currentItem.Next;
        }
        return -1;
    }
    public bool Contains(object element)
    {
        int index = indexOf(element);
        bool contains = (index != -1);
        return contains;
    }

    public void Clear()
    {
        this.head = null;
        this.tail = null;
        this.count = 0;
    }

    public object First()
    {
        if (this.count < 0)
        {
            throw new ArgumentOutOfRangeException("Out of range!");
        }
        else
            return this.head.Element;
    }

    public object Retrieve(int Position)
    {
        DoublyLinkedListNode current = this.head;

        for (int i = 0; i < Position && current != null; i++)
            current = current.Next;
        return current;
    }
}

class Program
{
    static void Main(string[] args)
    {

        DoublyLinkedList Parts = new DoublyLinkedList();
        object element;
        CarPart Part;
        CarPart PartToFind;

        Part = new CarPart();
        Part.PartNumber = 9743;
        Part.PartName = "Air Filter";
        Part.UnitPrice = 8.75;
        Parts.Add(Part);

        Part = new CarPart();
        Part.PartNumber = 27487;
        Part.PartName = "Clutch Disk";
        Part.UnitPrice = 47.15;
        Parts.Add(Part);

        Part = new CarPart();
        Part.PartNumber = 87873;
        Part.PartName = "Brake Disk";
        Part.UnitPrice = 35.15;
        Parts.Add(Part);

        Part = new CarPart();
        Part.PartNumber = 27644;
        Part.PartName = "A/C Filter Drier";
        Part.UnitPrice = 55.55;
        Parts.Add(Part);

        Console.WriteLine(" -=- Store Inventory -=-");
        Console.WriteLine("Number of Parts: {0}", Parts.Count);

        object item = (object)Parts;

        for (int i = 0; i < Parts.Count; i++)
        {
            // CarPart part = (CarPart)Parts.Retrieve(i);
            element = Parts.Retrieve(i);

            if (element != null)
            {
                // FAILS HERE AFTER RETRIEVING THE ELEMENT
                CarPart part = (CarPart)element;

                Console.WriteLine("\nCar Part Information");
                Console.WriteLine("Part #:      {0}", part.PartNumber);
                Console.WriteLine("Description: {0}", part.PartName);
                Console.WriteLine("Unit Price:  {0:C}", part.UnitPrice);
            }
        }
    }
}

我想要实现的是使用双向链表来存储许多不同的类型 - 在链表中添加项目没有问题,但是我尝试了许多不同的类型转换方法,但都失败了。

感谢任何关于应该解决的简单问题的建议。

1 个答案:

答案 0 :(得分:6)

您的Retrieve方法返回节点本身 - 而不是节点中的值,我假设它是您所期望的。这就是你获得InvalidCastException的原因。您需要更改的是Retrieve的最后一行

return current;

return current.element;

我还强烈建议您将DoublyLinkedList设为通用类型(即DoublyLinkedList<T>),此时您不需要投射,错误很明显在编译时。