需要使用反射将C#代码转换为HTML

时间:2011-01-11 17:39:44

标签: c# .net reflection

我最近发布了一个关于我正在编写的WCF Restful Web服务的问题,我需要将arbirtrary XSD作为输入,并将HTML表单作为输出流式传输。在其他用户的帮助下,我已经使用了XSD的方法,使用下面的代码从XSD动态创建C#文件,

CodeNamespace codeNamespace = new CodeNamespace("generated");
XmlCodeExporter codeExporter = new XmlCodeExporter(codeNamespace);

ArrayList maps = new ArrayList();
foreach (XmlSchemaType schemaType in xsd.SchemaTypes.Values)
{
    maps.Add(schemaImporter.ImportSchemaType(schemaType.QualifiedName));
}

foreach (XmlSchemaElement schemaElement in xsd.Elements.Values)
{
   maps.Add(schemaImporter.ImportTypeMapping(schemaElement.QualifiedName));
}
foreach(XmlTypeMapping map in maps)
{
    codeExporter.ExportTypeMapping(map);
}

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CodeParser parser = codeProvider.

using(StringWriter w = new StringWriter())
{
     codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, new      CodeGeneratorOptions());
     writer.Write(w.GetStringBuilder().ToString());
}

这样做是输出代表输入的XSD的C#文件的代码。这是我离开的地方。我被告知我现在可以使用反射从生成的C#代码生成HTML表单。

我有几个问题......一个是生成的代码包含多个部分类。这会影响使用反射吗?如果它不会影响它,那么最简单的方法是采用生成的代码并开始使用反射来生成我需要的输出?我不熟悉反思,也不知道我可以使用的工具,所以请以任何可能的方式提供帮助。虽然冗长,但我已经从创建的类中包含了生成的示例代码。我为长度道歉,但看到一切可能会有所帮助。

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] 
public partial class Leads { 
    private LeadsLead[] leadField;
    private ulong vidField; 
    private ulong lidField; 
    private ulong aidField; 
    private bool aidFieldSpecified; 
    private string passwordField; 
    [System.Xml.Serialization.XmlElementAttribute("Lead")]
    public LeadsLead[] Lead { get { return this.leadField; } set { this.leadField = value; } }
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public ulong vid { get { return this.vidField; } set { this.vidField = value; } }
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public ulong lid { get { return this.lidField; } set { this.lidField = value; } } 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public ulong aid { get { return this.aidField; } set { this.aidField = value; } } 
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool aidSpecified { get { return this.aidFieldSpecified; } set { this.aidFieldSpecified = value; } } 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string password { get { return this.passwordField; } set { this.passwordField = value; } } } 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")] 
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class LeadsLead { 
        private string firstNameField; 
        private string lastNameField; 
        private string addressField; 
        private string cityField; 
        private LeadsLeadState stateField;
        private string zipField; 
        private string emailField; 
        private string primaryPhoneField; 
        private string loanAmountField; 
        private string balanceField; 



[System.Xml.Serialization.XmlIgnoreAttribute()] 
        public bool LoanTypeSpecified { get { return this.loanTypeFieldSpecified; } set { this.loanTypeFieldSpecified = value; } } 
        public string EstimatedValue { get { return this.estimatedValueField; } set { this.estimatedValueField = value; } } 
        public string LenderDate { get { return this.lenderDateField; } set { this.lenderDateField = value; } }
        public string MobilePhone { get { return this.mobilePhoneField; } set { this.mobilePhoneField = value; } } 
        public bool OptOut { get { return this.optOutField; } set { this.optOutField = value; } } 
        [System.Xml.Serialization.XmlIgnoreAttribute()] 
        public bool OptOutSpecified { get { return this.optOutFieldSpecified; } set { this.optOutFieldSpecified = value; } } 
        public string SendComments { get { return this.sendCommentsField; } set { this.sendCommentsField = value; } } 
        public LeadsLeadVendorID VendorID { get { return this.vendorIDField; } set { this.vendorIDField = value; } } 
        [System.Xml.Serialization.XmlIgnoreAttribute()] 
        public bool VendorIDSpecified { get { return this.vendorIDFieldSpecified; } set { this.vendorIDFieldSpecified = value; } } 
        public System.DateTime CallTime { get { return this.callTimeField; } set { this.callTimeField = value; } } 
        [System.Xml.Serialization.XmlIgnoreAttribute()] 
        public bool CallTimeSpecified { get { return this.callTimeFieldSpecified; } set { this.callTimeFieldSpecified = value; } } 
        public string AdSource { get { return this.adSourceField; } set { this.adSourceField = value; } } 
        public LeadsLeadResult Result { get { return this.resultField; } set { this.resultField = value; } } 
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool ResultSpecified { get { return this.resultFieldSpecified; } set { this.resultFieldSpecified = value; } } 
        public decimal Duration { get { return this.durationField; } set { this.durationField = value; } } 
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool DurationSpecified { get { return this.durationFieldSpecified; } set { this.durationFieldSpecified = value; } } 
        public string ReviewStatus { get { return this.reviewStatusField; } set { this.reviewStatusField = value; } } 
        public string SubID { get { return this.subIDField; } set { this.subIDField = value; } } 
        public string Borrowers { get { return this.borrowersField; } set { this.borrowersField = value; } } 
        public string Loans { get { return this.loansField; } set { this.loansField = value; } } 
        [System.Xml.Serialization.XmlArrayItemAttribute("Driver", IsNullable=false)] 


public partial class LeadsLeadDriver { 
                private string firstNameField; 
            private string lastNameField;  
            public string FirstName { get { return this.firstNameField; } set { this.firstNameField = value; } }  
            public string LastName { get { return this.lastNameField; } set { this.lastNameField = value; } } }  
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")] 
        [System.SerializableAttribute()] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
        public enum LeadsLeadStandardState {  AL,  AK,  AZ,  AR,  CA,  CO,  CT,  DE,  DC,  FL,  GA,  HI,  ID,  IL,  IN,  IA,  KS,  KY,  LA,  ME,  MD,  MA,  MI,  MN,  MS,  MO,  MT,  NE,  NV,  NH,  NJ,  NM,  NY,  NC,  ND,  OH,  OK,  OR,  PA,  RI,  SC,  SD,  TN,  TX,  UT,  VT,  VA,  WA,  WV,  WI,  WY,  PR,  NS,  Alabama,  Alaska,  Arizona,  Arkansas,  California,  Colorado,  Connecticut,  Delaware,  [System.Xml.Serialization.XmlEnumAttribute("District of Columbia")] DistrictofColumbia,  Florida,  Georgia,  Hawaii,  Idaho,  Illinois,  Indiana,  Iowa,  Kansas,  Kentucky,  Louisiana,  Maine,  Maryland,  Massachusetts,  Michigan,  Minnesota,  Mississippi,  Missouri,  Montana,  Nebraska,  Nevada,  [System.Xml.Serialization.XmlEnumAttribute("New Hampshire")] NewHampshire,  [System.Xml.Serialization.XmlEnumAttribute("New Jersey")] NewJersey,  [System.Xml.Serialization.XmlEnumAttribute("New Mexico")] NewMexico,  [System.Xml.Serialization.XmlEnumAttribute("New York")] NewYork,  [System.Xml.Serialization.XmlEnumAttribute("North Carolina")] NorthCarolina,  [System.Xml.Serialization.XmlEnumAttribute("North Dakota")] NorthDakota,  Ohio,  Oklahoma,  Oregon,  Pennsylvania,  [System.Xml.Serialization.XmlEnumAttribute("Rhode Island")] RhodeIsland,  [System.Xml.Serialization.XmlEnumAttribute("South Carolina")] SouthCarolina,  [System.Xml.Serialization.XmlEnumAttribute("South Dakota")] SouthDakota,  Tennessee,  Texas,  Utah,  Vermont,  Virginia,  Washington,  [System.Xml.Serialization.XmlEnumAttribute("West Virginia")] WestVirginia,  Wisconsin,  Wyoming,  [System.Xml.Serialization.XmlEnumAttribute("Puerto Rico")] PuertoRico,  [System.Xml.Serialization.XmlEnumAttribute("No State")] NoState, } 
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")] 
        [System.SerializableAttribute()] 
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
        public enum LeadsLeadStatus {  Pending,  Working,  Closed,  Dead, } 

2 个答案:

答案 0 :(得分:4)

  

我有几个问题......一个是生成的代码包含多个部分类。这会影响使用反射吗?

我在你的所有文字中都看到了一个问题,就是这样。答案是否定的,在源中定义了一个类,因为partial根本不会影响反射。 partial只是一个源代码“技巧”,如果愿意,它允许您跨多个源文件拆分类或方法的定义。编译器在最后将这些多个定义组合成一个类或方法,反射看不到,也不会受到这个事实的影响。

答案 1 :(得分:1)

在编译时组合部分类 - 对生成的类的反射根本不会受到影响。