我正在使用C#和.NET Framework爲AutoCAD 2014編寫一個插件。我擴展Autodesk的Table
類,像這樣:將父類投射到C#中的子類中#
public class OpeningDataTable : Autodesk.AutoCAD.DatabaseServices.Table
的想法是,我想拉已經繪製在AutoCAD繪圖出來的圖紙爲OpeningDataTable
實例的表,所以我可以操縱與方法,數據I」已經寫了。我這樣做,就像這樣:
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;
}
}
不過,我得到一個錯誤說,我不能一個Table
對象(父類)轉換爲OpeningDataTable
對象(子類)。
這個問題有一個簡單的解決方法嗎?
號你可以不投了'''到除非B'的了''實際類型是'乙'或者繼承'B'的東西。在這裏,變量的實際類型是'Table',並且您試圖將其轉換爲'OpeningDataTable',其中'Table'不會繼承'OpeningDataTable'。 – MarcinJuraszek 2014-11-01 20:37:11