您好,登錄后才能下訂單哦!
這篇文章給大家介紹C# 中Employee對象的作用是什么,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
C# Employee對象
命令的名字是PRINTOUTEMPLOYEE。ListEmployee()函數接收一個ObjectId參數,它通過一個ref類型的字符串數組返回值(包含相應的雇員數據)。調用它的PrintoutEmployee()函數只是用來在命令行中輸出這些數據。
我們需要一個遍歷并顯示所有雇員數據的命令。
public static void ListEmployee(ObjectId employeeId, ref string[] saEmployeeList)
{
int nEmployeeDataCount = 0;
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction(); //開始事務處理。
try
{
Entity ent = (Entity)trans.GetObject(employeeId, OpenMode.ForRead, false);
//打開當前對象!
if (ent.GetType() == typeof(BlockReference))
{
//不是所有的塊索引都有雇員數據,所以我們要處理錯誤
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; //出現了錯誤……字典或擴展記錄不能訪問
}
if (bHasOurDict) //如果獲得擴展字典,而又有擴展記錄……
{
// 為雇員列表分配內存
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;
//現在,讓我們從公司字典中獲取老板的名字
//在NOD中找到.
DBDictionary NOD = (DBDictionary)trans.GetObject(db.NamedObjectsDictionaryId,
OpenMode.ForRead, false);DBDictionary acmeDict = (DBDictionary)trans.GetObject(NOD.GetAt("ACME_DIVISION"),
OpenMode.ForRead);//注意我們直接使用擴展數據...
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);//***,把雇員的數據輸出到命令行
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);//現在,我們需要把內容輸出到命令行。這里可以有一個對象幫助我們:
//下面的部分,我們將遍歷模型空間:
foreach (ObjectId id in btr)
{
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false); //打開當前對象!
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
{
}
}
關于C# 中Employee對象的作用是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。