2013-04-03 66 views
2

我有一个类,像这样:奇怪的问题

public class CompanyData 
    { 
     # region Properties 
     /// <summary> 
     /// string CompanyNumber 
     /// </summary> 
     private string strCompanyNumber;  

     /// <summary> 
     /// string CompanyName 
     /// </summary> 
     private string strCompanyName; 

    [Info("companynumber")] 
    public string CompanyNumber 
    { 
     get 
     { 
      return this.strCompanyNumber; 
     } 

     set 
     { 
      this.strCompanyNumber = value; 
     } 
    } 

    /// <summary> 
    /// Gets or sets CompanyName 
    /// </summary> 
    [Info("companyName")] 
    public string CompanyName 
    { 
     get 
     { 
      return this.strCompanyName; 
     } 

     set 
     { 
      this.strCompanyName = value; 
     } 
    } 

    /// <summary> 
    /// Initializes a new instance of the CompanyData class 
    /// </summary> 
    public CompanyData() 
    { 
    } 

    /// <summary> 
    /// Initializes a new instance of the CompanyData class 
    /// </summary> 
    /// <param name="other"> object company data</param> 
    public CompanyData(CompanyData other) 
    { 
     this.Init(other); 
    } 

    /// <summary> 
    /// sets the Company data attributes 
    /// </summary> 
    /// <param name="other">object company data</param> 
    protected void Init(CompanyData other) 
    { 
     this.CompanyNumber = other.CompanyNumber; 
     this.CompanyName = other.CompanyName; 
    } 

    /// <summary> 
    /// Getting array of entity properties 
    /// </summary> 
    /// <returns>An array of PropertyInformation</returns> 
    public PropertyInfo[] GetEntityProperties() 
    { 
     PropertyInfo[] thisPropertyInfo; 

     thisPropertyInfo = this.GetType().GetProperties(); 

     return thisPropertyInfo; 
    } 

    } 

CSV文件读取和CompanyData对象的集合创建

在这种方法中,我试图让性能和值:

private void GetPropertiesAndValues(List<CompanyData> resList) 
{ 

     foreach (CompanyData resRow in resList) 
     { 

      this.getProperties = resRow.ExternalSyncEntity.GetEntityProperties(); 

      this.getValues = resRow.ExternalSyncEntity.GetEntityValue(resRow.ExternalSyncEntity); 

     } 
} 

这里的问题,对于第一对象的GetEntityProperties()返回CompanyNumber为阵列中的第一个元素。对于其余对象,它返回CompanyName作为第一个元素。

为什么序列不一致?

问候。

回答

4

Type.GetProperties()不返回有序的结果。

的的GetProperties方法不以特定的顺序返回的属性,如字母或声明顺序。您的代码不得依赖于返回属性的顺序,因为顺序会有所不同。

如果要使用命令/一致的结果,这是更好地返回的数组排序。

+0

一种常用的方法是在属性上引入某种排序属性,即[Order(int)] – ghord

+0

或按名称排序。 –