我真的想知道是否可行。
班级
public class XLRSV : AuditableEntity<long>
{
[DisplayName("INDEX")]
public int IDX { get; set; }
.
.
30 more
.
.
[DisplayName("REMOTE STATUS")]
public int EPSR_REMOTE_STATUS { get; set; }
[DisplayName("SCAN STATUS")]
public int EPSR_SCAN_STATUS { get; set; }
}
存储库
IEnumerable<XLRSV> GetScanData(int id);
控制器
var result = _xlrsvSvr.GetScanData(id).FirstOrDefault();
Normaly使用
int idx = result.IDX;
所以,我想在此
上使用make list变量var column = from name in typeof(XLRSV).GetProperties()
select name;
List<object> columnName = new List<object>();
List<object> columnDisplayName = new List<object>();
MemberInfo cname;
MemberInfo cDispName;
foreach (var item in column)
{
cname = typeof(XLRSV).GetProperty(item.Name);
columnName.Add(cname.Name);
cDispName = typeof(XLRSV).GetProperty(cname.Name);
columnDisplayName.Add(cDispName.CustomAttributes.Select(x => x.NamedArguments.Select(y => y.TypedValue.Value)).OfType<System.ComponentModel.DataAnnotations.DisplayAttribute>());
}
现在我正在使用此代码
new {name = "IDX", displayName = columnDisplayName[0].ToString(), result=result.IDX, checkPoint=""},
new {name = "EPSR_UNIQUE_NUMBER", displayName = columnDisplayName[1].ToString(), result=result.EPSR_UNIQUE_NUMBER, checkPoint=""},
.
.over 30 more
.
new {name = "EPSR_SCAN_STATUS", displayName = columnDisplayName[22].ToString(), result="", checkPoint=""},
所以,我想使用像
这样的循环for (int i = 0; i< columnName.count()) {
new {
name = columnName[i].toString(),
displayName = columnDisplayName[i].ToString(),
result=result.columnName[i].toString(), //here i want to use varialbe
checkPoint=""
},
}
我找不到方法。它可以吗?
答案 0 :(得分:0)
你的问题不是最明确的,但我相信我理解你想要完成的事情。我相信这是一个简单的反思问题。除了DisplayName
之外,你似乎还有其他所需的东西。
我已经创建了一个功能,您可以将其放在XLRSV
课程和课程上,以帮助您检索我认为您想要的信息。有关每个功能/类目的的说明,请参阅注释。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
// This will grab the desired info for each public instance property on the class.
// Put this function inside your XLRSV. If you can't change XLRSV adjust this to be
// a method extension.
public IEnumerable<ObjectPropertyInfo<XLRSV>> GetObjectPropertyInfo()
{
var type = typeof(XLRSV);
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var list = new List<ObjectPropertyInfo<XLRSV>>();
foreach (var property in properties)
{
list.Add(new ObjectPropertyInfo<XLRSV>(property, this));
}
return list;
}
// This will represent the data you want and given the property info and object
// will retrieve and set all the values as needed. It appears you had most of this
// figured out other than the DisplayName which is retrieved via GetCustomAttribute.
public class ObjectPropertyInfo<T> where T : class
{
public string Name { get; set; }
public string DisplayName { get; set; }
public string Result { get; set; }
public string CheckPoint { get; set; }
/// <summary>
/// Constructs a ClassInfo given a property name and an object from which to retrieve the value.
/// </summary>
public ObjectPropertyInfo(PropertyInfo propInfo, T obj)
{
if (propInfo == null) { throw new ArgumentNullException("propInfo was null."); }
Name = propInfo.Name;
DisplayName = GetDisplayName(propInfo);
Result = GetValue(propInfo, obj);
CheckPoint = "";
}
/// <summary>
/// Retrieves the display name of the property if one is available.
/// </summary>
private string GetDisplayName(PropertyInfo propInfo)
{
if (propInfo == null) { return null; }
var displayNameAttr = propInfo.GetCustomAttribute<DisplayNameAttribute>();
if (displayNameAttr == null) { return propInfo.Name; }
return displayNameAttr.DisplayName;
}
/// <summary>
/// Retrieves the value of an object given which property.
/// </summary>
private string GetValue(PropertyInfo propInfo, T obj)
{
if (propInfo == null) { return null; }
return propInfo.GetValue(obj)?.ToString();
}
}
我希望这就是你要找的东西。