简体   繁体   English

在C#中将父类强制转换为子类

[英]Casting a parent class to a child class in C#

I'm writing a plug-in for AutoCAD 2014 using C# and the .NET Framework. 我正在使用C#和.NET Framework编写AutoCAD 2014插件。 I extended Autodesk's Table class like so: 我像这样扩展了Autodesk的Table类:

public class OpeningDataTable : Autodesk.AutoCAD.DatabaseServices.Table

The idea is that I want to pull a table already drawn on an AutoCAD drawing out of the drawing as an instance of OpeningDataTable so I can manipulate the data with methods I've written. 我的想法是,我想从绘图中拉出已经在AutoCAD绘图上绘制的表作为OpeningDataTable的实例,以便可以使用编写的方法来操作数据。 I am doing that like so: 我这样做是这样的:

OpeningDataTable myTable = checkForExistingTable(true);

public Autodesk.AutoCAD.DatabaseServices.Table checkForExistingTable(bool isWindow)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; //Current drawing
            Transaction tr = doc.TransactionManager.StartTransaction();
            DocumentLock docLock = doc.LockDocument();
            TypedValue[] tableItem = new TypedValue[] { new TypedValue(0, "ACAD_TABLE") };
            SelectionFilter tableSelecFilter = new SelectionFilter(tableItem);
            Editor ed = doc.Editor; //Editor object to ask user where table goes, subclass of Document

            using (tr)
            {
                PromptSelectionResult selectResult = ed.SelectAll(tableSelecFilter);
                if (selectResult.Status == PromptStatus.OK)
                {
                    SelectionSet tableSelSet = selectResult.Value;
                    for (int i = 0; i < tableSelSet.Count; i++)
                    {
                        Autodesk.AutoCAD.DatabaseServices.Table tableToCheck = (Autodesk.AutoCAD.DatabaseServices.Table)tr.GetObject(tableSelSet[i].ObjectId, OpenMode.ForRead);
                        String tableTitle = tableToCheck.Cells[0, 0].Value.ToString();
                        if(tableTitle.Equals("Window Schedule") && isWindow == true)
                            return (OpeningDataTable)tableToCheck;
                        if (tableTitle.Equals("Door Schedule") && isWindow == false)
                            return (OpeningDataTable)tableToCheck;
                    }
                }
                return null;
            }
        }

However, I get an error saying that I cannot convert a Table object (the parent class) to an OpeningDataTable object (the child class). 但是,我收到一条错误消息,说我无法将Table对象(父类)转换为OpeningDataTable对象(子类)。

Is there a simple workaround for this issue? 有没有解决此问题的简单方法?

You will need to create a constructor for OpeningDataTable that takes Table as a parameter. 您将需要为OpeningDataTable创建一个使用Table作为参数的构造函数。

The reason that you cannot cast a Table to an OpeningDataTable is that a Table is not an OpeningDataTable just like an object is not an int . 无法将Table OpeningDataTable转换为OpeningDataTable是, Table不是像object不是int一样的OpeningDataTable

You cannot downcast a reference to an object like that, unless it is truly a reference to an object of the child class. 除非它确实是对子类对象的引用,否则您不能向下转换对此类对象的引用。 For example, the following is fine... 例如,以下情况很好...

string foo = "Yup!";
object fooToo = foo;
string fooey = fooToo as string;

Console.WriteLine(fooey);  // prints Yup!

...because fooToo is simply referencing a String as an Object : so the downcast works. ...因为fooToo只是将String引用为Object所以向下转换有效。

Consider using the Decorator design pattern and adding a constructor to OpeningDataTable that accepts a Table arg: 考虑使用Decorator设计模式 ,并向OpeningDataTable添加一个接受Table arg的构造函数:

public class OpeningDataTable : Autodesk.AutoCAD.DatabaseServices.Table
{
    private Table _table; // the decorated Table

    public OpeningDataTable(Table table)
    {
        _table = table;
    }

    // <your methods for working with the decorated Table>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM