C# 在C中使用PropertyInfo时出现的奇怪问题#
我有一门课是这样的:C# 在C中使用PropertyInfo时出现的奇怪问题#,c#,.net,reflection,types,C#,.net,Reflection,Types,我有一门课是这样的: public class CompanyData { # region Properties /// <summary> /// string CompanyNumber /// </summary> private string strCompanyNumber; /// <summary> /// stri
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;
}
}
公共类公司数据
{
#区域属性
///
///字符串公司编号
///
私有字符串strCompanyNumber;
///
///字符串公司名
///
私有字符串strCompanyName;
[信息(“公司编号”)]
公共字符串公司编号
{
收到
{
返回此.strCompanyNumber;
}
设置
{
this.strCompanyNumber=值;
}
}
///
///获取或设置CompanyName
///
[信息(“公司名称”)]
公共字符串公司名
{
收到
{
返回this.strCompanyName;
}
设置
{
this.strCompanyName=值;
}
}
///
///初始化CompanyData类的新实例
///
上市公司数据()
{
}
///
///初始化CompanyData类的新实例
///
///目标公司数据
公共公司数据(公司数据其他)
{
本.Init(其他);
}
///
///设置公司数据属性
///
///目标公司数据
受保护的void Init(公司数据其他)
{
this.CompanyNumber=other.CompanyNumber;
this.CompanyName=other.CompanyName;
}
///
///获取实体属性数组
///
///一系列属性信息
公共属性信息[]GetEntityProperties()
{
PropertyInfo[]此PropertyInfo;
thisPropertyInfo=this.GetType().GetProperties();
将该物业归还信息;
}
}
读取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);
}
}
private void获取属性和值(列表resList)
{
foreach(resList中的CompanyData resRow)
{
this.getProperties=resRow.ExternalSyncEntity.GetEntityProperties();
this.getValues=resRow.ExternalSyncEntity.GetEntityValue(resRow.ExternalSyncEntity);
}
}
问题是,对于第一个对象,GetEntityProperties()
返回CompanyNumber
作为数组中的第一个元素。对于其余的对象,它返回CompanyName
作为第一个元素
为什么序列不一致
注意。不会返回已排序的结果
GetProperties方法不按特定顺序返回属性,如字母顺序或声明顺序。您的代码不能依赖于返回属性的顺序,因为该顺序是不同的
如果要使用有序/一致结果,最好对返回的数组进行排序。该数组不返回有序结果
GetProperties方法不按特定顺序返回属性,如字母顺序或声明顺序。您的代码不能依赖于返回属性的顺序,因为该顺序是不同的
如果要使用有序/一致的结果,最好对返回的数组进行排序。常用的方法是对属性[Order(int)]引入某种排序属性或按名称进行排序。常用的方法是对属性[Order(int)]引入某种排序属性或按名称进行排序。