2011-01-11 29 views
0

我最近發佈了一個關於WCF Restful Web服務的問題,我正在編寫這個服務,我需要將一個Arbirtrary XSD作爲輸入,並將HTML表單作爲輸出流。從其他用戶一些幫助後,我已經與使用XSD動態創建使用下面的代碼的XSD一個C#文件的方法,,,需要使用反射將C#代碼轉換爲HTML

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, } 

回答

4

我有幾個擔心......一個是生成的代碼包含多個部分類。這會影響使用反射嗎?

我在所有的文本中看到一個問題,就是這樣。答案是否定的,因爲partial根本不會影響反思。如果你願意,partial只是一個源代碼「技巧」,它允許你跨多個源文件拆分類或方法的定義。編譯器將這些多個定義結合到一個類或方法的最後,反射看不到,也不會受到這個事實的影響。

+0

謝謝,這是很好的知道它不會影響最終解決方案 – TheJediCowboy 2011-01-11 17:56:56

1

部分類在編譯時結合 - 對生成的類的反射根本不會受到影響。

相關問題