1
개체 목록을 가져온 다음 속성 및 특성 유형을 기반으로 모든 속성 값을 반환하는 메서드가 있습니다.
선택은 조건에 따라 다르므로 값을 선택하는 데 어려움이 있습니다. 지금까지 내가 본 가장 추한 방법으로 값을 얻기 위해 나를 수 했는가
: Linq의 Select 절 내의 조건
public IEnumerable<string> GetValues(List<ProperyInfo> objects)
{
var allValues = new List<string>();
foreach (var obj in objects)
{
// Get the PropertyInfo for the obj
var properties = GetPropertiesWithTheAttributes(obj);
var values = properties.Select(x => new
{
Value = x.GetValue(obj, null) == null
? string.empty
:
x.PropertyType.BaseType == typeof(Enum)
? Convert.ToInt32(x.GetValue(obj, null)).ToString()
: (x.GetValue(obj, null)).ToString(),
((ReportAttribute)
Attribute.GetCustomAttribute(x, typeof(ReportAttribute), false)).Order
})
.OrderBy(x => x.Order)
.Select(x => x.Value.ToString())
.ToList();
allValues.AddRange(values);
}
return allValues;
}
그리고 내가 여기에 게시 된 코드에서 나는 심지어 ReportAttribute에있는 DisplayDate 속성에 대한 체크를 제거
하는 속성의 datetime 속성이 날짜 또는 날짜로 표시되는지 확인합니다.평온한 지금! 당신은
public IEnumerable<string> GetValues(List<PropertyInfo> objects)
{
var allValues = new List<string>();
foreach (var obj in objects)
{
// Get the PropertyInfo for the obj
var properties = GetPropertiesWithTheAttributes(obj);
var values = properties.Select(x => new
{
Value = GetValue(obj, x),
Order = GetOrder(x)
})
.OrderBy(x => x.Order)
.Select(x => x.Value.ToString())
.ToList();
allValues.AddRange(values);
}
return allValues;
}
당신이 정말이 인라인하려면 :
// not sure how to name 'obj' and 'x' without more context
private static object GetValue(PropertyInfo obj, PropertyInfo x)
{
if (x.GetValue(obj, null) == null) return string.Empty;
if (x.PropertyType.BaseType == typeof (Enum))
return Convert.ToInt32(x.GetValue(obj, null));
return x.GetValue(obj, null);
}
private static int GetOrder(PropertyInfo x)
{
return ((ReportAttribute) Attribute.GetCustomAttribute(x, typeof(ReportAttribute), false)).Order;
}
그래서 당신이 쓸 수 있습니다 :
+1 그 참으로 D 문제를 해결하십시오! 고마워,하지만 내부에 조건을 쓰는 방법이 있니? 답변을 추가하면 승인 된 답변으로 표시됩니다. 다시 한 번 감사드립니다. – gdoron
람다 식을 명령문 (편집 참조)으로 변경할 수는 있지만 권장하지 않습니다. 별도의 메소드는보다 읽기 쉽고 확실히 디버그하기 쉽습니다. – jeroenh
아니요, 지식을 위해서만 인라인하고 싶지 않습니다 ... 다시 한 번 감사드립니다. – gdoron