[英]How to get the attribute name on a model?
如何在我创建的模型上获得服装的名字?
例如对于这种情况ID_HR,SURNAME,NAME,我尝试使用GetProperties,但是它不起作用
public class Person
{
public string ID_HR;
public string SURNAME;
public string NAME;
public string GENDER;
public string N_GENDER;
public DateTime? DT_BIRTH;
}
我尝试了这个
Type parent = typeof(VSM_Data);
FieldInfo[] children = parent.GetFields();
for (int i = 0; i < children.Length; i++)
{
Type child = children[i].GetType();
var columnnamesChild = from t in child.GetProperties() select t.Name;
foreach (var item in columnnamesChild)
{
DragAndDrop FundDragAndDrop = new DragAndDrop();
FundDragAndDrop.TITLE = item;
FundDragAndDrop.adresse = "{{PERSON." + children[i].Name.ToUpper() + "." + item.ToUpper() + "}}";
FundList.Add(FundDragAndDrop);
}
和这个
FieldInfo[] myPropertyInfo = children[i].GetType().GetFields(BindingFlags.Public);
for (int a = 0; a < myPropertyInfo.Length; a++)
{
DragAndDrop FundDragAndDrop = new DragAndDrop();
FundDragAndDrop.TITLE = myPropertyInfo.ToString();
FundDragAndDrop.adresse = "{{PERSON." + children[i].Name.ToUpper() + "." + myPropertyInfo.ToString().ToUpper() + "}}";
FundList.Add(FundDragAndDrop);
}
这是因为您声明的不是属性,而是简单的成员变量。 GetProperties
不适用于成员变量。 使用它来获取属性:
public class Person
{
public string ID_HR { get; set; }
public string SURNAME { get; set; }
public string NAME { get; set; }
public string GENDER { get; set; }
public string N_GENDER { get; set; }
public DateTime? DT_BIRTH { get; set; }
}
您需要使用get
和set
声明您的属性,否则它将仅是member variable
。 尝试这样:
public class Person
{
public string ID_HR { get; set; }
public string SURNAME { get; set; }
public string NAME { get; set; }
public string GENDER { get; set; }
public string N_GENDER { get; set; }
public DateTime? DT_BIRTH { get; set; }
}
然后使用GetProperties方法。
如果您将VMS_Data
此类
class VMS_Data{
....
Person p;
Country c;
....
}
然后当你做
Type child = children[i].GetType();
您尝试从FieldInfo
获取类型。 您需要像这样使用FieldType
Type child = children[i].FieldType;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.