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

1 回答 1

4

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

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

如果要使用有序/一致的结果,最好对返回的数组进行排序。

于 2013-04-03T06:49:45.983 回答