110,538
社区成员
发帖
与我相关
我的任务
分享
public class OrdinalAttribute : Attribute
{
private int ordinal;
public OrdinalAttribute(int ordinal)
{
this.ordinal = ordinal;
}
public int Ordinal
{
get { return this.ordinal; }
set { this.ordinal = value; }
}
}
public class TestType
{
[Ordinal(0)]
public int ZZZ = 0;
[Ordinal(1)]
public int UserId { get; set; }
[Ordinal(2)]
public int BBB { get; set; }
[Ordinal(3)]
public int AAA { get; set; }
}
class Program
{
public static int GetOrdinal(System.Reflection.MemberInfo member)
{
try
{
var attrs = member.GetCustomAttributes(typeof(OrdinalAttribute), false).Cast<OrdinalAttribute>();
return attrs.First().Ordinal;
}
catch
{
return 0;
}
}
static void Main(string[] args)
{
var result = typeof(TestType).GetMembers()
.Where(m => m.MemberType == System.Reflection.MemberTypes.Field || m.MemberType == System.Reflection.MemberTypes.Property)
.Select(m => new
{
Member = m,
Ordinal = GetOrdinal(m)
})
.OrderBy(m => m.Ordinal);
foreach (var item in result)
{
Console.WriteLine("{0} - {1}", item.Member.Name, item.Ordinal);
}
}
}