2012-08-22 69 views
-1

我有一個由VS2010創建的XSD.exe類。如何使用C#中的反射訪問類中的每個字段的值?

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:4.0.30319.269 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

using System.Xml.Serialization; 

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1. 
// 

namespace ConsoleApplication1 
{ 
    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "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 transfer 
    { 

     private string sourceGLNField; 

     private string destinationGLNField; 

     private string actionTypeField; 

     private string shipToField; 

     private string documentNumberField; 

     private string documentDateField; 

     private string noteField; 

     private string versionField; 

     private transferCarrier[] carrierField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public string sourceGLN 
     { 
      get 
      { 
       return this.sourceGLNField; 
      } 
      set 
      { 
       this.sourceGLNField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public string destinationGLN 
     { 
      get 
      { 
       return this.destinationGLNField; 
      } 
      set 
      { 
       this.destinationGLNField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public string actionType 
     { 
      get 
      { 
       return this.actionTypeField; 
      } 
      set 
      { 
       this.actionTypeField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public string shipTo 
     { 
      get 
      { 
       return this.shipToField; 
      } 
      set 
      { 
       this.shipToField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public string documentNumber 
     { 
      get 
      { 
       return this.documentNumberField; 
      } 
      set 
      { 
       this.documentNumberField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public string documentDate 
     { 
      get 
      { 
       return this.documentDateField; 
      } 
      set 
      { 
       this.documentDateField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public string note 
     { 
      get 
      { 
       return this.noteField; 
      } 
      set 
      { 
       this.noteField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public string version 
     { 
      get 
      { 
       return this.versionField; 
      } 
      set 
      { 
       this.versionField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute("carrier", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public transferCarrier[] carrier 
     { 
      get 
      { 
       return this.carrierField; 
      } 
      set 
      { 
       this.carrierField = value; 
      } 
     } 
    } 

    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
    public partial class transferCarrier 
    { 

     private transferCarrierProductList[] productListField; 

     private string carrierLabelField; 

     private string containerTypeField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute("productList", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public transferCarrierProductList[] productList 
     { 
      get 
      { 
       return this.productListField; 
      } 
      set 
      { 
       this.productListField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlAttributeAttribute()] 
     public string carrierLabel 
     { 
      get 
      { 
       return this.carrierLabelField; 
      } 
      set 
      { 
       this.carrierLabelField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlAttributeAttribute()] 
     public string containerType 
     { 
      get 
      { 
       return this.containerTypeField; 
      } 
      set 
      { 
       this.containerTypeField = value; 
      } 
     } 
    } 

    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
    public partial class transferCarrierProductList 
    { 

     private transferCarrierProductListSerialNumber[] serialNumberField; 

     private string expirationDateField; 

     private string lotNumberField; 

     private string gTINField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute("serialNumber", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = true)] 
     public transferCarrierProductListSerialNumber[] serialNumber 
     { 
      get 
      { 
       return this.serialNumberField; 
      } 
      set 
      { 
       this.serialNumberField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlAttributeAttribute()] 
     public string expirationDate 
     { 
      get 
      { 
       return this.expirationDateField; 
      } 
      set 
      { 
       this.expirationDateField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlAttributeAttribute()] 
     public string lotNumber 
     { 
      get 
      { 
       return this.lotNumberField; 
      } 
      set 
      { 
       this.lotNumberField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlAttributeAttribute()] 
     public string GTIN 
     { 
      get 
      { 
       return this.gTINField; 
      } 
      set 
      { 
       this.gTINField = value; 
      } 
     } 
    } 

    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
    public partial class transferCarrierProductListSerialNumber 
    { 

     private string valueField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlTextAttribute()] 
     public string Value 
     { 
      get 
      { 
       return this.valueField; 
      } 
      set 
      { 
       this.valueField = value; 
      } 
     } 
    } 

    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "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 NewDataSet 
    { 

     private transfer[] itemsField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute("transfer")] 
     public transfer[] Items 
     { 
      get 
      { 
       return this.itemsField; 
      } 
      set 
      { 
       this.itemsField = value; 
      } 
     } 
    } 

} 

而且我有這樣一個控制檯應用程序:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.IO; 
using System.Xml.Serialization; 
using System.Xml.Schema; 
using System.Reflection; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //Load xml file 
      using (FileStream xmlStream = new FileStream("C:\\SaleReturn01.xml", FileMode.Open)) 
      { 
       //Read xml file 
       using (XmlReader xmlReader = XmlReader.Create(xmlStream)) 
       { 
        //Serialize xml with class which is created by XSD.exe in visual studio and set class with xmlfile 
        //In that case we don't need to xsd referenced validation 
        XmlSerializer serializer1 = new XmlSerializer(typeof(transfer)); 
        transfer deserializedStudents = serializer1.Deserialize(xmlReader) as transfer; 
        showTransferClass(deserializedStudents); 
       } 
      } 
     } 

     public static void showTransferClass(transfer x) 
     { 
      try 
      { 
       FieldInfo[] myFields = typeof(transfer).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); 
       foreach (FieldInfo field in myFields) 
       { 
        if (!field.FieldType.IsArray) 
        { 
         Console.WriteLine("{0} : {1}", field.Name, field.GetValue(x)); 
        } 
        else 
        { 
         Console.WriteLine("{0} : {1}", field.Name, ""); 
         foreach (transferCarrier y in x.carrier) 
         { 
          showTransferCarierClass(y); 
         } 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("Exception : {0}", e.Message); 
      } 
     } 

     public static void showTransferCarierClass(transferCarrier x) 
     { 
      try 
      { 
       FieldInfo[] myFields = typeof(transferCarrier).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); 
       foreach (FieldInfo field in myFields) 
       { 
        if (!field.FieldType.IsArray) 
        { 
         Console.WriteLine(" {0} : {1}", field.Name, field.GetValue(x)); 
        } 
        else 
        { 
         Console.WriteLine(" {0} : {1}", field.Name, ""); 
         foreach (transferCarrierProductList y in x.productList) 
         { 
          showTransferCarierProductListClass(y); 
         } 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(" Exception : {0}", e.Message); 
      } 
     } 

     public static void showTransferCarierProductListClass(transferCarrierProductList x) 
     { 
      try 
      { 
       FieldInfo[] myFields = typeof(transferCarrierProductList).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); 
       foreach (FieldInfo field in myFields) 
       { 
        if (!field.FieldType.IsArray) 
        { 
         Console.WriteLine(" {0} : {1}", field.Name, field.GetValue(x)); 
        } 
        else 
        { 
         Console.WriteLine(" {0} : {1}", field.Name, ""); 
         foreach (transferCarrierProductListSerialNumber y in x.serialNumber) 
         { 
          showTransferCarierProductListSerialNumberClass(y); 

         } 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(" Exception : {0}", e.Message); 
      } 
     } 

     public static void showTransferCarierProductListSerialNumberClass(transferCarrierProductListSerialNumber x) 
     { 
      try 
      { 
       FieldInfo[] myFields = typeof(transferCarrierProductListSerialNumber).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); 
       foreach (FieldInfo field in myFields) 
       { 
        if (!field.FieldType.IsArray) 
        { 
         Console.WriteLine("  {0} : {1}", field.Name, field.GetValue(x)); 
        } 
        else 
        { 
         Console.WriteLine("  {0} : {1}", field.Name, ""); 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("  Exception : {0}", e.Message); 
      } 
     } 
    } 
} 

所有的一切,我可以得到字段的名字,但每次我試圖領域的get值時的異常拋出這樣的。 (謝謝Jon Skeet)

你有什麼想法來改進這段代碼嗎?

+0

「我得到了一個通用類」 - 我看不到有關泛型的證據...... –

回答

1

嗯,是......你打電話:

temp = fld.GetValue(null); 

從文檔的FieldInfo.GetValue

如果該字段是靜態的,OBJ被忽略。對於非靜態字段,obj應該是繼承或聲明字段的類的實例。

那麼,什麼實例的類型是你試圖顯示的字段?將其作爲參數傳遞給GetValue。沒有一個實例,「領域的價值」的概念沒有任何意義。這就像說「String.Length的價值是什麼?」而不涉及特定的字符串。

(另請注意,如果您是.NET命名約定,也如果你只宣佈在第一次使用的變量點,限制它們的範圍你的代碼會更清晰。)

相關問題