employee(C# 中Employee对象的作用是什么)

发布时间:2025-12-10 22:48:07 浏览次数:1

C# Employee对象

命令的名字是PRINTOUTEMPLOYEE。ListEmployee()函数接收一个ObjectId参数,它通过一个ref类型的字符串数组返回值(包含相应的雇员数据)。调用它的PrintoutEmployee()函数只是用来在命令行中输出这些数据。

我们需要一个遍历并显示所有雇员数据的命令。

  1. publicstaticvoidListEmployee(ObjectIdemployeeId,refstring[]saEmployeeList)

  2. {

  3. intnEmployeeDataCount=0;

  4. Databasedb=HostApplicationServices.WorkingDatabase;

  5. Transactiontrans=db.TransactionManager.StartTransaction();//开始事务处理。

  6. try

  7. {

  8. Entityent=(Entity)trans.GetObject(employeeId,OpenMode.ForRead,false);

  9. //打开当前对象!

  10. if(ent.GetType()==typeof(BlockReference))

  11. {

  12. //不是所有的块索引都有雇员数据,所以我们要处理错误

  13. boolbHasOurDict=true;

  14. XrecordEmployeeXRec=null;

  15. try{

  16. BlockReferencebr=(BlockReference)ent;

  17. DBDictionaryextDict=(DBDictionary)trans.GetObject
    (br.ExtensionDictionary,OpenMode.ForRead,false);

  18. EmployeeXRec=(Xrecord)trans.GetObject(extDict.GetAt("EmployeeData"),
    OpenMode.ForRead,false);

  19. }

  20. catch

  21. {

  22. bHasOurDict=false;//出现了错误……字典或扩展记录不能访问

  23. }

  24. if(bHasOurDict)//如果获得扩展字典,而又有扩展记录……

  25. {

  26. //为雇员列表分配内存

  27. saEmployeeList=newString[4];

  28. //加入雇员的名字

  29. TypedValueresBuf=EmployeeXRec.Data.AsArray()[0];

  30. saEmployeeList.SetValue(string.Format("{0}\n",resBuf.Value),
    nEmployeeDataCount);

  31. nEmployeeDataCount+=1;

  32. //加入雇员的薪水

  33. resBuf=EmployeeXRec.Data.AsArray()[1];

  34. saEmployeeList.SetValue(string.Format("{0}\n",resBuf.Value),
    nEmployeeDataCount);

  35. nEmployeeDataCount+=1;

  36. //加入雇员所在的部门

  37. resBuf=EmployeeXRec.Data.AsArray()[2];

  38. stringstr=(string)resBuf.Value;

  39. saEmployeeList.SetValue(string.Format("{0}\n",resBuf.Value),
    nEmployeeDataCount);

  40. nEmployeeDataCount+=1;

  41. //现在,让我们从公司字典中获取老板的名字

  42. //在NOD中找到.

  43. DBDictionaryNOD=(DBDictionary)trans.GetObject(db.NamedObjectsDictionaryId,
    OpenMode.ForRead,false);

  44. DBDictionaryacmeDict=(DBDictionary)trans.GetObject(NOD.GetAt("ACME_DIVISION"),
    OpenMode.ForRead);

  45. //注意我们直接使用扩展数据...

  46. DBDictionarysalesDict=(DBDictionary)trans.GetObject(acmeDict.GetAt
    ((string)EmployeeXRec.Data.AsArray()[2].Value),OpenMode.ForRead);

  47. XrecordsalesXRec=(Xrecord)trans.GetObject(salesDict.GetAt("DepartmentManager"),
    OpenMode.ForRead);

  48. //***,把雇员的数据输出到命令行

  49. resBuf=salesXRec.Data.AsArray()[0];

  50. saEmployeeList.SetValue(string.Format("{0}\n",resBuf.Value),nEmployeeDataCount);

  51. nEmployeeDataCount+=1;

  52. }

  53. }

  54. trans.Commit();

  55. }

  56. finally

  57. {

  58. trans.Dispose();

  59. }

  60. }

  61. [CommandMethod("PRINTOUTEMPLOYEE")]

  62. publicstaticvoidPrintoutEmployee()

  63. {

  64. Editored=Application.DocumentManager.MdiActiveDocument.Editor;

  65. //声明我们将在下面使用的工具...

  66. Databasedb=HostApplicationServices.WorkingDatabase;

  67. Transactiontrans=db.TransactionManager.StartTransaction();

  68. try

  69. {

  70. //首先,获取块表和模型空间块表记录

  71. BlockTablebt=(BlockTable)trans.GetObject(HostApplicationServices.
    WorkingDatabase.BlockTableId,OpenMode.ForRead);

  72. BlockTableRecordbtr=(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],
    OpenMode.ForRead);

  73. //现在,我们需要把内容输出到命令行。这里可以有一个对象帮助我们:

  74. //下面的部分,我们将遍历模型空间:

  75. foreach(ObjectIdidinbtr)

  76. {

  77. Entityent=(Entity)trans.GetObject(id,OpenMode.ForRead,false);//打开当前对象!

  78. if(entisBlockReference)

  79. {

  80. string[]saEmployeeList=null;//这是正确的...定义新的列表。

  81. ListEmployee(id,refsaEmployeeList);

  82. if((saEmployeeList.Length==4))

  83. {

  84. ed.WriteMessage("EmployeeName:{0}",saEmployeeList[0]);

  85. ed.WriteMessage("EmployeeSalary:{0}",saEmployeeList[1]);

  86. ed.WriteMessage("EmployeeDivision:{0}",saEmployeeList[2]);

  87. ed.WriteMessage("DivisionManager:{0}",saEmployeeList[3]);

  88. }

  89. }

  90. }

  91. }

  92. finally

  93. {

  94. }

  95. }

关于C# 中Employee对象的作用是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

employee
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477