C# Employee對象淺談
C#語言有很多值得學(xué)習(xí)的地方,這里我們主要介紹C# Employee對象,包括介紹所有雇員數(shù)據(jù)的命令等方面。
C# Employee對象
本文用代碼片斷演示了怎樣獲取C# Employee對象的所有內(nèi)容,包括ACME_DIVISION字典中的部門經(jīng)理的名字。如果有時間的話,請閱讀一下其中的代碼來看看它是怎么使用的。它可以被直接放到你的類中并可以運行。命令的名字是PRINTOUTEMPLOYEE。ListEmployee()函數(shù)接收一個ObjectId參數(shù),它通過一個ref類型的字符串?dāng)?shù)組返回值(包含相應(yīng)的雇員數(shù)據(jù))。調(diào)用它的PrintoutEmployee()函數(shù)只是用來在命令行中輸出這些數(shù)據(jù)。
我們需要一個遍歷并顯示所有雇員數(shù)據(jù)的命令。
- public static void ListEmployee(ObjectId employeeId, ref string[] saEmployeeList)
 - {
 - int nEmployeeDataCount = 0;
 - Database db = HostApplicationServices.WorkingDatabase;
 - Transaction trans = db.TransactionManager.StartTransaction(); //開始事務(wù)處理。
 - try
 - {
 - Entity ent = (Entity)trans.GetObject(employeeId, OpenMode.ForRead, false);
 - //打開當(dāng)前對象!
 - if (ent.GetType() == typeof(BlockReference))
 - {
 - //不是所有的塊索引都有雇員數(shù)據(jù),所以我們要處理錯誤
 - bool bHasOurDict = true;
 - Xrecord EmployeeXRec = null;
 - try{
 - BlockReference br = (BlockReference)ent;
 - DBDictionary extDict = (DBDictionary)trans.GetObject
 
(br.ExtensionDictionary, OpenMode.ForRead, false);- EmployeeXRec = (Xrecord)trans.GetObject(extDict.GetAt("EmployeeData"),
 
OpenMode.ForRead, false);- }
 - catch
 - {
 - bHasOurDict = false; //出現(xiàn)了錯誤……字典或擴展記錄不能訪問
 - }
 - if (bHasOurDict) //如果獲得擴展字典,而又有擴展記錄……
 - {
 - // 為雇員列表分配內(nèi)存
 - saEmployeeList = new String[4];
 - //加入雇員的名字
 - TypedValue resBuf = EmployeeXRec.Data.AsArray()[0];
 - saEmployeeList.SetValue(string.Format("{0}\n", resBuf.Value),
 
nEmployeeDataCount);- nEmployeeDataCount += 1;
 - //加入雇員的薪水
 - resBuf = EmployeeXRec.Data.AsArray()[1];
 - saEmployeeList.SetValue(string.Format("{0}\n", resBuf.Value),
 
nEmployeeDataCount);- nEmployeeDataCount += 1;
 - //加入雇員所在的部門
 - resBuf = EmployeeXRec.Data.AsArray()[2];
 - string str = (string)resBuf.Value;
 - saEmployeeList.SetValue(string.Format("{0}\n", resBuf.Value),
 
nEmployeeDataCount);- nEmployeeDataCount += 1;
 - //現(xiàn)在,讓我們從公司字典中獲取老板的名字
 - //在NOD中找到.
 - DBDictionary NOD = (DBDictionary)trans.GetObject(db.NamedObjectsDictionaryId,
 
OpenMode.ForRead, false);- DBDictionary acmeDict = (DBDictionary)trans.GetObject(NOD.GetAt("ACME_DIVISION"),
 
OpenMode.ForRead);- //注意我們直接使用擴展數(shù)據(jù)...
 - DBDictionary salesDict = (DBDictionary)trans.GetObject(acmeDict.GetAt
 
((string)EmployeeXRec.Data.AsArray()[2].Value),OpenMode.ForRead);- Xrecord salesXRec = (Xrecord)trans.GetObject(salesDict.GetAt("Department Manager"),
 
OpenMode.ForRead);- //***,把雇員的數(shù)據(jù)輸出到命令行
 - resBuf = salesXRec.Data.AsArray()[0];
 - saEmployeeList.SetValue(string.Format("{0}\n", resBuf.Value), nEmployeeDataCount);
 - nEmployeeDataCount += 1;
 - }
 - }
 - trans.Commit();
 - }
 - finally
 - {
 - trans.Dispose();
 - }
 - }
 - [CommandMethod("PRINTOUTEMPLOYEE")]
 - public static void PrintoutEmployee()
 - {
 - Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
 - //聲明我們將在下面使用的工具...
 - Database db = HostApplicationServices.WorkingDatabase;
 - Transaction trans = db.TransactionManager.StartTransaction();
 - try
 - {
 - //首先,獲取塊表和模型空間塊表記錄
 - BlockTable bt = (BlockTable)trans.GetObject(HostApplicationServices.
 
WorkingDatabase.BlockTableId, OpenMode.ForRead);- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],
 
OpenMode.ForRead);- //現(xiàn)在,我們需要把內(nèi)容輸出到命令行。這里可以有一個對象幫助我們:
 - //下面的部分,我們將遍歷模型空間:
 - foreach (ObjectId id in btr)
 - {
 - Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false); //打開當(dāng)前對象!
 - if (ent is BlockReference)
 - {
 - string[] saEmployeeList = null;// 這是正確的...定義新的列表。
 - ListEmployee(id, ref saEmployeeList);
 - if ((saEmployeeList.Length == 4))
 - {
 - ed.WriteMessage("Employee Name: {0}", saEmployeeList[0]);
 - ed.WriteMessage("Employee Salary: {0}", saEmployeeList[1]);
 - ed.WriteMessage("Employee Division: {0}", saEmployeeList[2]);
 - ed.WriteMessage("Division Manager: {0}", saEmployeeList[3]);
 - }
 - }
 - }
 - }
 - finally
 - {
 - }
 - }
 
【編輯推薦】















 
 
 
 
 
 
 