从 C# 中的 class 获取所有属性名称
Get all the attributes name from a class in C#
我有一个 class 名称,c_invPrd 代码如下。
public readonly string ITEM_NO = "ITEM_NO";
public readonly string PRD_INDEX = "PRD_INDEX";
public readonly string PRD_CD = "PRD_CD";
public readonly string PRD_DESC = "PRD_DESC";
public readonly string PRINCIPAL_IND = "PRINCIPAL_IND";
public readonly string PROMO_IND = "PROMO_IND";
public readonly string PRDCAT1_CD = "PRDCAT1_CD";
public readonly string PRDCAT2_CD = "PRDCAT2_CD";
public readonly string PRDCAT3_CD = "PRDCAT3_CD";
public readonly string PRDCAT4_CD = "PRDCAT4_CD";
public readonly string PRDCAT5_CD = "PRDCAT5_CD";
public readonly string PRDCAT6_CD = "PRDCAT6_CD";
public readonly string PRDCAT7_CD = "PRDCAT7_CD";
public readonly string PRDCAT8_CD = "PRDCAT8_CD";
public readonly string PRDCAT9_CD = "PRDCAT9_CD";
public readonly string PRDCAT10_CD = "PRDCAT10_CD";
private readonly Type typeof_ITEM_NO = typeof(int);
private readonly Type typeof_PRD_INDEX = typeof(int);
private readonly Type typeof_PRD_CD = typeof(string);
private readonly Type typeof_PRD_DESC = typeof(string);
private readonly Type typeof_PRINCIPAL_IND = typeof(string);
private readonly Type typeof_PROMO_IND = typeof(string);
private readonly Type typeof_PRDCAT1_CD = typeof(string);
private readonly Type typeof_PRDCAT2_CD = typeof(string);
private readonly Type typeof_PRDCAT3_CD = typeof(string);
private readonly Type typeof_PRDCAT4_CD = typeof(string);
private readonly Type typeof_PRDCAT5_CD = typeof(string);
private readonly Type typeof_PRDCAT6_CD = typeof(string);
private readonly Type typeof_PRDCAT7_CD = typeof(string);
private readonly Type typeof_PRDCAT8_CD = typeof(string);
private readonly Type typeof_PRDCAT9_CD = typeof(string);
private readonly Type typeof_PRDCAT10_CD = typeof(string);
public c_invIndPrd(DMSFW.Objects.Argument arg)
: base (arg) {
}
public c_invIndPrd()
{
}
如何创建一个 C# 列表来放置所有属性,如 ITEM_NO、PRD_INDEX、PRD_CD、PRD_DESC、PRINCIPAL_IND 等通过访问下面的 c_invPrd class 进入列表?我正在使用 sharpdeveloper 作为我的 IDE.
这段代码看起来非常非常奇怪 - 看起来您想要一些私有字段,但只是 string
个:
var fields = typeof(c_invPrd).GetFields(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.Where(f => f.FieldType == typeof(string)).ToArray();
如果您只想要名字:
var fields = typeof(c_invPrd).GetFields(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.Where(f => f.FieldType == typeof(string)).Select(f => f.Name).ToArray();
如果你想要值那么你需要一个类型的实例,然后使用FieldInfo.GetValue
:
c_invPrd obj = ...
var fields = typeof(c_invPrd).GetFields(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.Where(f => f.FieldType == typeof(string))
.Select(f => (string)f.GetValue(obj)).ToArray();
坦率地说,这段代码令人费解。这些东西应该是const
(对于string
)或static readonly
(对于Type
)——或者它们应该是常规属性,即
public int ITEM_NO {get;set;} // I'd use ItemNumber if it was me, but...
public string PRD_CD {get;set;} // I'm going to guess... ProductCode?
我有一个 class 名称,c_invPrd 代码如下。
public readonly string ITEM_NO = "ITEM_NO";
public readonly string PRD_INDEX = "PRD_INDEX";
public readonly string PRD_CD = "PRD_CD";
public readonly string PRD_DESC = "PRD_DESC";
public readonly string PRINCIPAL_IND = "PRINCIPAL_IND";
public readonly string PROMO_IND = "PROMO_IND";
public readonly string PRDCAT1_CD = "PRDCAT1_CD";
public readonly string PRDCAT2_CD = "PRDCAT2_CD";
public readonly string PRDCAT3_CD = "PRDCAT3_CD";
public readonly string PRDCAT4_CD = "PRDCAT4_CD";
public readonly string PRDCAT5_CD = "PRDCAT5_CD";
public readonly string PRDCAT6_CD = "PRDCAT6_CD";
public readonly string PRDCAT7_CD = "PRDCAT7_CD";
public readonly string PRDCAT8_CD = "PRDCAT8_CD";
public readonly string PRDCAT9_CD = "PRDCAT9_CD";
public readonly string PRDCAT10_CD = "PRDCAT10_CD";
private readonly Type typeof_ITEM_NO = typeof(int);
private readonly Type typeof_PRD_INDEX = typeof(int);
private readonly Type typeof_PRD_CD = typeof(string);
private readonly Type typeof_PRD_DESC = typeof(string);
private readonly Type typeof_PRINCIPAL_IND = typeof(string);
private readonly Type typeof_PROMO_IND = typeof(string);
private readonly Type typeof_PRDCAT1_CD = typeof(string);
private readonly Type typeof_PRDCAT2_CD = typeof(string);
private readonly Type typeof_PRDCAT3_CD = typeof(string);
private readonly Type typeof_PRDCAT4_CD = typeof(string);
private readonly Type typeof_PRDCAT5_CD = typeof(string);
private readonly Type typeof_PRDCAT6_CD = typeof(string);
private readonly Type typeof_PRDCAT7_CD = typeof(string);
private readonly Type typeof_PRDCAT8_CD = typeof(string);
private readonly Type typeof_PRDCAT9_CD = typeof(string);
private readonly Type typeof_PRDCAT10_CD = typeof(string);
public c_invIndPrd(DMSFW.Objects.Argument arg)
: base (arg) {
}
public c_invIndPrd()
{
}
如何创建一个 C# 列表来放置所有属性,如 ITEM_NO、PRD_INDEX、PRD_CD、PRD_DESC、PRINCIPAL_IND 等通过访问下面的 c_invPrd class 进入列表?我正在使用 sharpdeveloper 作为我的 IDE.
这段代码看起来非常非常奇怪 - 看起来您想要一些私有字段,但只是 string
个:
var fields = typeof(c_invPrd).GetFields(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.Where(f => f.FieldType == typeof(string)).ToArray();
如果您只想要名字:
var fields = typeof(c_invPrd).GetFields(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.Where(f => f.FieldType == typeof(string)).Select(f => f.Name).ToArray();
如果你想要值那么你需要一个类型的实例,然后使用FieldInfo.GetValue
:
c_invPrd obj = ...
var fields = typeof(c_invPrd).GetFields(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.Where(f => f.FieldType == typeof(string))
.Select(f => (string)f.GetValue(obj)).ToArray();
坦率地说,这段代码令人费解。这些东西应该是const
(对于string
)或static readonly
(对于Type
)——或者它们应该是常规属性,即
public int ITEM_NO {get;set;} // I'd use ItemNumber if it was me, but...
public string PRD_CD {get;set;} // I'm going to guess... ProductCode?