p; m_Class.Members.Add(xmlfile);
// 根据
模板文件产生保存字段值的变量
String fieldname = "", fieldtype = "";
foreach (XmlNode xn in m_Xml.DocumentElement.ChildNodes)
{
fieldname = "m_" + xn.Name;
fieldtype = xn.Attributes["type"].Value;
CodeMemberField field = new CodeMemberField(fieldtype, fieldname);
m_Class.Members.Add(field);
}
// 产生 "private bool m_AddFlag;"
CodeMemberField addflag = new CodeMemberField("System.Boolean", "m_AddFlag");
m_Class.Members.Add(addflag);
}
在以上代码中每段程序上方的注释是它们所生成的C#源代码。在输入这段代码之前,需要引入两个namespace。
using System.CodeDom;
using System.CodeDom.Compiler;
五、属性的生成
在数据表类中每个属性代表数据表的一个字段,名子就是字段名。这些属性和保存字段的全局变量一一对应。下面是具体的实现代码:
private void GenerateProperties()
{
String fieldname = "", fieldtype = "";
foreach (XmlNode xn in m_Xml.DocumentElement.ChildNodes)
{
fieldname = xn.Name;
fieldtype = xn.Attributes["type"].Value;
CodeMemberProperty property = new CodeMemberProperty();
property.Attributes = MemberAttributes.Public | MemberAttributes.Final;
property.Name = fieldname;
property.Type = new CodeTypeReference(fieldtype);
property.HasGet = true;
property.HasSet = true;
CodeVariableReferenceExpression field = new CodeVariableReferenceExpression("m_" + fieldname);
// 产生 return m_property
CodeMethodReturnStatement propertyReturn = new CodeMethodReturnStatement(field);
property.GetStatements.Add(propertyReturn);
&nbs