开发者

Need to use reflection to turn C# code into HTML

I recently posted a question about a WCF Restful web service that I am writing that I need to take an arbirtrary XSD as an input, and to stream an HTML form as the output. After some help from other users, I have gone with the method to use the XSD to dynamically create a C# file from the XSD using the code below,,,

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());
}

What this is doing is outputting the code for the C# file that would represent the XSD that was inputted. This is where I have left off. I was informed that I can now use reflection to generate the HTML form from the generated C# code.

I have a couple concerns...one being the generated code contains multiple partial classes. Will this affect using reflection at all? If it wont affect it, then what is the easiest way to take the code that is generated and begin using reflection on that to generate the output i need? I am new to reflection and am not aware of the tools that I have available to me, so please help out in any way possible. Although lengthy, I have included the generated example code from the class that was created. I apologize about the length, but It might help to see everything.

[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 s开发者_如何学Pythontring 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, } 


I have a couple concerns...one being the generated code contains multiple partial classes. Will this affect using reflection at all?

I see one question in all of your text, and this is it. The answer is no, that a class is defined in source as partial does not impact reflection at all. partial is merely a source code "trick", if you will, that allows you to split the definition of a class or method across multiple source files. The compiler combines these multiple definitions into one class or method at the end and reflection can't see and is not impacted by that fact.


Partial classes are combined at compile time- reflection over the generated classes will not be affected at all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜