2

オブジェクトのプロパティの順序を指定するために CustomAttributes を利用しようとしています

 public class WCG : DistrictExport
    {
        [DataMember(Name = "Survey", Order = 190)]
        public string Survey { get; set; }
        [DataMember(Name = "Western Hemisphere\nwith Europe", Order = 200)]
        public string WesternHemisphereWithEurope { get; set; }
        [DataMember(Name = "Eastern Hemisphere", Order = 210)]
        public string EasternHemisphere { get; set; }
    }

新しいオブジェクトを作成せずに doddlereport の列の順序を指定するにはどうすればよいですか?

List<object> report = GetReportResults();
report = new Report(results.ToReportSource(), Writer);
4

1 に答える 1

0

OK!,私はあなたの問題を試します.そして、私はうまくいきました.私のソースを試してください:

私の単純なテストクラス:

public class Test
{
    [DataMember(Name = "A", Order = 96)]
    public string A { get; set; }

    [DataMember(Name = "B", Order = 97)]
    public string B { get; set; }

    [DataMember(Name = "C", Order = 98)]
    public string C { get; set; }
}

そして内線:

 public static class Ext
    {

        public static IList<KeyValuePair<string, int>> AsOrderColumns<T>(this T t)
            where T : class
        {
            return t.GetType()
                    .GetProperties()
                    .Where(w => w.IsOrderColumn())
                    .Select(s => s.GetOrderColumn())
                    .OrderBy(o => o.Value)
                    .ToList();

        }

        private static bool IsOrderColumn(this PropertyInfo prop)
        {
            return prop.GetCustomAttributes(typeof(DataMemberAttribute), true)
                       .Any();
        }

        private static KeyValuePair<string, int> GetOrderColumn(this PropertyInfo prop)
        {
            var attr = prop.GetCustomAttributes(typeof(DataMemberAttribute), true)
                           .ElementAt(0) as DataMemberAttribute;

            return (attr != null)
                ? new KeyValuePair<string, int>(attr.Name, attr.Order)
                : new KeyValuePair<string, int>();
        }

        public static IList<object> AsOrderRow<T>(this T t)
            where T : class
        {
            return t.GetType()
                    .GetProperties()
                    .Where(w => w.IsOrderColumn())
                    .OrderBy(o => o.GetOrderColumn().Value)
                    .Select(s => s.GetValue(t, null))
                    .ToList();
        }

    }

コンソール テスト コード:

 class Program
    {
        static void Main(string[] args)
        {
            var test = new Test();
            var tests = new List<Test>()
                {
                    new Test() {A = "A-1.1", B = "B-1.2", C = "C-1.3"}, 
                    new Test() {A = "A-2.1", B = "B-2.2", C = "C-2.3"}, 
                    new Test() {A = "A-3.1", B = "B-3.2", C = "C-3.3"}, 
                    new Test() {A = "A-4.1", B = "B-4.2", C = "C-4.3"}
                };

            Console.WriteLine(String.Join<string>("\t", test.AsOrderColumns().Select(s => String.Format("{0}({1})", s.Key, s.Value))));

            foreach (var item in tests)
            {
                Console.WriteLine(String.Join<object>("\t", item.AsOrderRow()));
            }

            Console.ReadKey();

        }
    }
于 2013-04-26T16:39:57.513 に答える