发布时间:2025-12-10 22:48:07 浏览次数:1
C# Employee对象
命令的名字是PRINTOUTEMPLOYEE。ListEmployee()函数接收一个ObjectId参数,它通过一个ref类型的字符串数组返回值(包含相应的雇员数据)。调用它的PrintoutEmployee()函数只是用来在命令行中输出这些数据。
我们需要一个遍历并显示所有雇员数据的命令。
publicstaticvoidListEmployee(ObjectIdemployeeId,refstring[]saEmployeeList)
{
intnEmployeeDataCount=0;
Databasedb=HostApplicationServices.WorkingDatabase;
Transactiontrans=db.TransactionManager.StartTransaction();//开始事务处理。
try
{
Entityent=(Entity)trans.GetObject(employeeId,OpenMode.ForRead,false);
//打开当前对象!
if(ent.GetType()==typeof(BlockReference))
{
//不是所有的块索引都有雇员数据,所以我们要处理错误
boolbHasOurDict=true;
XrecordEmployeeXRec=null;
try{
BlockReferencebr=(BlockReference)ent;
DBDictionaryextDict=(DBDictionary)trans.GetObject
(br.ExtensionDictionary,OpenMode.ForRead,false);EmployeeXRec=(Xrecord)trans.GetObject(extDict.GetAt("EmployeeData"),
OpenMode.ForRead,false);}
catch
{
bHasOurDict=false;//出现了错误……字典或扩展记录不能访问
}
if(bHasOurDict)//如果获得扩展字典,而又有扩展记录……
{
//为雇员列表分配内存
saEmployeeList=newString[4];
//加入雇员的名字
TypedValueresBuf=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];
stringstr=(string)resBuf.Value;
saEmployeeList.SetValue(string.Format("{0}\n",resBuf.Value),
nEmployeeDataCount);nEmployeeDataCount+=1;
//现在,让我们从公司字典中获取老板的名字
//在NOD中找到.
DBDictionaryNOD=(DBDictionary)trans.GetObject(db.NamedObjectsDictionaryId,
OpenMode.ForRead,false);DBDictionaryacmeDict=(DBDictionary)trans.GetObject(NOD.GetAt("ACME_DIVISION"),
OpenMode.ForRead);//注意我们直接使用扩展数据...
DBDictionarysalesDict=(DBDictionary)trans.GetObject(acmeDict.GetAt
((string)EmployeeXRec.Data.AsArray()[2].Value),OpenMode.ForRead);XrecordsalesXRec=(Xrecord)trans.GetObject(salesDict.GetAt("DepartmentManager"),
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")]
publicstaticvoidPrintoutEmployee()
{
Editored=Application.DocumentManager.MdiActiveDocument.Editor;
//声明我们将在下面使用的工具...
Databasedb=HostApplicationServices.WorkingDatabase;
Transactiontrans=db.TransactionManager.StartTransaction();
try
{
//首先,获取块表和模型空间块表记录
BlockTablebt=(BlockTable)trans.GetObject(HostApplicationServices.
WorkingDatabase.BlockTableId,OpenMode.ForRead);BlockTableRecordbtr=(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForRead);//现在,我们需要把内容输出到命令行。这里可以有一个对象帮助我们:
//下面的部分,我们将遍历模型空间:
foreach(ObjectIdidinbtr)
{
Entityent=(Entity)trans.GetObject(id,OpenMode.ForRead,false);//打开当前对象!
if(entisBlockReference)
{
string[]saEmployeeList=null;//这是正确的...定义新的列表。
ListEmployee(id,refsaEmployeeList);
if((saEmployeeList.Length==4))
{
ed.WriteMessage("EmployeeName:{0}",saEmployeeList[0]);
ed.WriteMessage("EmployeeSalary:{0}",saEmployeeList[1]);
ed.WriteMessage("EmployeeDivision:{0}",saEmployeeList[2]);
ed.WriteMessage("DivisionManager:{0}",saEmployeeList[3]);
}
}
}
}
finally
{
}
}
关于C# 中Employee对象的作用是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。