将对象类型转换为C#类

本文关键字:类型转换 对象 | 更新日期: 2023-09-27 18:21:49

问题:我试图将对象类型类型转换回根类
我从一个通用的双链接列表开始,我可以添加值,但不能检索

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);
            }
        }
    }
}

我试图实现的是使用双链表来存储许多不同的类型——向链表中添加项目没有问题,但我尝试了许多不同的打字方式,但都失败了。

感谢您就本应解决的简单问题提供任何建议。

将对象类型转换为C#类

您的Retrieve方法返回节点本身,而不是节点内的值,我认为这是您期望的值。这就是为什么您得到InvalidCastException。您只需要更改中Retrieve的最后一行

return current;

return current.element;

我还强烈建议您将DoublyLinkedList设置为泛型类型(即DoublyLinkedList<T>),这样您就不需要强制转换,而且在编译时错误会很明显。