私はいくつかのモデルのコンテンツをリストして保存し、それをリストに追加する必要があるプロジェクトに取り組んでいます。ここでモデルリストからすべての値の名前を取得するにはどうすればいいですか?
は私のモデルである:
public class ProductPage
{
public string Name { get; set; }
public string Reference { get; set; }
public string Color { get; set; }
}
そしてここでは私のコードです:
public List<TechnicalAttributes> GetAttributeFromProduct(ProductPage product)
{
List<TechnicalAttributes> attributeList = new List<TechnicalAttributes>();
foreach (PropertyInfo pi in product.GetType().GetProperties())
{
if (pi.PropertyType == typeof(string))
{
string value = (string)pi.GetValue(product);
if (!string.IsNullOrEmpty(value))
{
if (!Helper.IsNumeric(value))
{
attributeList.Add(new TechnicalAttributes() { Name = GetVariableName(() => value), Value = value });
}
}
}
}
return attributeList;
}
私のコードは私がこれを取得することができます:
type = "value" - attribute = "myName"
type = "value" - attribute = "myReference"
type = "value" - attribute = "myColor"
を、私は何かをしたいですこのように:
type = "Name" - attribute = "myName"
type = "Reference" - attribute = "myReference"
type = "Color" - attribute = "myColor"
モデルにもっと多くのパラメータがあるので、foreachを使用する必要があります。 誰かが私を助けることができますか?
私は、 'GetVariableName'を行うことになっているか知っている が、何が必要ですありません:[MemberInfo.Name](https://msdn.microsoft.com/en-us/library/system .reflection.memberinfo.name(v = vs.110).aspx)( 'PropertyInfo'は' MemberInfo'を継承します) –