這篇文章給大家介紹C# 中Employee對(duì)象的作用是什么,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)江口免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千多家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
C# Employee對(duì)象
命令的名字是PRINTOUTEMPLOYEE。ListEmployee()函數(shù)接收一個(gè)ObjectId參數(shù),它通過(guò)一個(gè)ref類型的字符串?dāng)?shù)組返回值(包含相應(yīng)的雇員數(shù)據(jù))。調(diào)用它的PrintoutEmployee()函數(shù)只是用來(lái)在命令行中輸出這些數(shù)據(jù)。
我們需要一個(gè)遍歷并顯示所有雇員數(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)前對(duì)象!
if (ent.GetType() == typeof(BlockReference))
{
//不是所有的塊索引都有雇員數(shù)據(jù),所以我們要處理錯(cuò)誤
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)了錯(cuò)誤……字典或擴(kuò)展記錄不能訪問(wèn)
}
if (bHasOurDict) //如果獲得擴(kuò)展字典,而又有擴(kuò)展記錄……
{
// 為雇員列表分配內(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);//注意我們直接使用擴(kuò)展數(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)容輸出到命令行。這里可以有一個(gè)對(duì)象幫助我們:
//下面的部分,我們將遍歷模型空間:
foreach (ObjectId id in btr)
{
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false); //打開當(dāng)前對(duì)象!
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
{
}
}
關(guān)于C# 中Employee對(duì)象的作用是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。