怎样根据图示中蓝色部分[DataFileAttribute("CU-DESC1","")]来获取
红色[Description("Customer Description 1")]部分的内容?
请大神讲详细一点,别让我又去百度啥的,就是因为百度不到才来请教你们呢!谢谢了

C#实体类中获取有关类容
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- threenewbee 2018-07-11 13:15关注
以后回答问题请把代码贴完整。回答你一个问题,我还得手敲代码,你故意麻烦回答者,当然没人愿意搭理你了。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Q694451 { class DescriptionAttribute : Attribute { public string Description { get; set; } public DescriptionAttribute(string s) { Description = s; } } class DataFileAttribute : Attribute { public string FieldName { get; set; } public string FieldType { get; set; } public DataFileAttribute(string s, string t) { FieldName = s; FieldType = t; } } class ModelCustomer { [DataFileAttribute("a", "nvchar")] [Description("a")] public string a { get; set; } [DataFileAttribute("b", "nvchar")] [Description("b")] public string b { get; set; } [DataFileAttribute("c", "nvchar")] [Description("c")] public string c { get; set; } [DataFileAttribute("d", "nvchar")] [Description("d")] public string d { get; set; } [DataFileAttribute("CU-DESC1", "")] [Description("Customer Description 1")] public string CustomDescription1 { get; set; } } class Program { static void Main(string[] args) { var query = typeof(ModelCustomer).GetProperties().Select(x => new { k = (Attribute.GetCustomAttribute(x, typeof(DataFileAttribute)) as DataFileAttribute) ?? new DataFileAttribute("", ""), v = (Attribute.GetCustomAttribute(x, typeof(DescriptionAttribute)) as DescriptionAttribute) ?? new DescriptionAttribute("") }).Single(x => x.k.FieldName == "CU-DESC1").v.Description; Console.WriteLine(query); } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报