C# Employee对象浅谈

开发 后端
本文用代码片断演示了怎样获取C# Employee对象的所有内容,包括ACME_DIVISION字典中的部门经理的名字。

C#语言有很多值得学习的地方,这里我们主要介绍C# Employee对象,包括介绍所有雇员数据的命令等方面。

C# Employee对象

本文用代码片断演示了怎样获取C# Employee对象的所有内容,包括ACME_DIVISION字典中的部门经理的名字。如果有时间的话,请阅读一下其中的代码来看看它是怎么使用的。它可以被直接放到你的类中并可以运行。命令的名字是PRINTOUTEMPLOYEE。ListEmployee()函数接收一个ObjectId参数,它通过一个ref类型的字符串数组返回值(包含相应的雇员数据)。调用它的PrintoutEmployee()函数只是用来在命令行中输出这些数据。

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

  1. public static void ListEmployee(ObjectId employeeId, ref string[] saEmployeeList)  
  2. {  
  3. int nEmployeeDataCount = 0;  
  4. Database db = HostApplicationServices.WorkingDatabase;  
  5. Transaction trans = db.TransactionManager.StartTransaction(); //开始事务处理。  
  6. try  
  7. {  
  8. Entity ent = (Entity)trans.GetObject(employeeId, OpenMode.ForRead, false); 
  9. //打开当前对象!  
  10. if (ent.GetType() == typeof(BlockReference))  
  11. {  
  12. //不是所有的块索引都有雇员数据,所以我们要处理错误  
  13. bool bHasOurDict = true;  
  14. Xrecord EmployeeXRec = null;  
  15. try{  
  16. BlockReference br = (BlockReference)ent;  
  17. DBDictionary extDict = (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 = new String[4];  
  28. //加入雇员的名字  
  29. TypedValue resBuf = 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. string str = (string)resBuf.Value;  
  39. saEmployeeList.SetValue(string.Format("{0}\n", resBuf.Value), 
    nEmployeeDataCount);  
  40. nEmployeeDataCount += 1;  
  41. //现在,让我们从公司字典中获取老板的名字  
  42. //在NOD中找到.  
  43. DBDictionary NOD = (DBDictionary)trans.GetObject(db.NamedObjectsDictionaryId, 
    OpenMode.ForRead, false);  
  44. DBDictionary acmeDict = (DBDictionary)trans.GetObject(NOD.GetAt("ACME_DIVISION"), 
    OpenMode.ForRead);  
  45. //注意我们直接使用扩展数据...  
  46. DBDictionary salesDict = (DBDictionary)trans.GetObject(acmeDict.GetAt
    ((string)EmployeeXRec.Data.AsArray()[2].Value),OpenMode.ForRead);  
  47. Xrecord salesXRec = (Xrecord)trans.GetObject(salesDict.GetAt("Department Manager"), 
    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.  
  62. [CommandMethod("PRINTOUTEMPLOYEE")]  
  63. public static void PrintoutEmployee()  
  64. {  
  65. Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;  
  66. //声明我们将在下面使用的工具...  
  67. Database db = HostApplicationServices.WorkingDatabase;  
  68. Transaction trans = db.TransactionManager.StartTransaction();  
  69. try  
  70. {  
  71. //首先,获取块表和模型空间块表记录  
  72. BlockTable bt = (BlockTable)trans.GetObject(HostApplicationServices.
    WorkingDatabase.BlockTableId, OpenMode.ForRead);  
  73. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], 
    OpenMode.ForRead);  
  74. //现在,我们需要把内容输出到命令行。这里可以有一个对象帮助我们:  
  75. //下面的部分,我们将遍历模型空间:  
  76. foreach (ObjectId id in btr)  
  77. {  
  78. Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false); //打开当前对象!  
  79. if (ent is BlockReference)  
  80. {  
  81. string[] saEmployeeList = null;// 这是正确的...定义新的列表。  
  82. ListEmployee(id, ref saEmployeeList);  
  83. if ((saEmployeeList.Length == 4))  
  84. {  
  85. ed.WriteMessage("Employee Name: {0}", saEmployeeList[0]);  
  86. ed.WriteMessage("Employee Salary: {0}", saEmployeeList[1]);  
  87. ed.WriteMessage("Employee Division: {0}", saEmployeeList[2]);  
  88. ed.WriteMessage("Division Manager: {0}", saEmployeeList[3]);  
  89. }  
  90. }  
  91. }  
  92. }  
  93. finally  
  94. {  
  95. }  

【编辑推荐】

  1. C#创建快捷方式简单描述
  2. C#压缩Access数据库详细介绍
  3. C#实现加载动态库概述
  4. C#日期型数据简单剖析
  5. C#装箱和拆箱简单描述
责任编辑:佚名 来源: 赛迪网
相关推荐

2009-08-31 09:44:23

C# Employee

2009-08-19 17:12:18

C# Connecti

2009-08-12 11:24:25

C# String对象

2009-09-02 15:41:21

C# HTTPWebR

2009-08-18 09:06:41

C#对象和集合

2009-09-02 16:36:37

C#调用Excel对象

2009-07-31 17:51:27

C#对象初始化

2009-05-08 09:46:37

微软C#集合对象

2009-08-26 15:28:52

C#对象集合初始化器

2009-08-03 15:06:43

C# Stack对象C# Queue对象

2011-09-21 10:56:31

C#结构

2009-08-20 18:30:33

C# ReaderWr

2009-08-07 11:26:53

C#数组结构

2009-08-19 16:19:33

Employee对象

2012-03-14 10:48:05

C#

2009-08-26 13:15:38

C#选择控制

2009-08-26 15:46:01

C#匿名类型

2009-08-14 17:58:05

C#接口方法

2009-08-20 10:24:52

C#开发WinForm

2009-08-25 16:16:43

C# oledbcon
点赞
收藏

51CTO技术栈公众号