0

使用以下代码,我正在尝试创建 KeyValuePair 列表。然而它总是空的。为什么?

在调试期间,您可以看到属性名称中包含单词 Role。我还检查了类型 tblJob,所有这些属性都是字符串。见截图。

tblJob job = (tblJob)entity.Entity;

var listOfRolesnamesAndValues = 
    typeof(tblJob) //Get all properties that contains the Role name on it.
    .GetProperties(BindingFlags.Public)
    .Where(p => p.Name.Contains("Role") && p.PropertyType == typeof(string))
    .Select(p => new KeyValuePair<string, string>(p.Name, p.GetValue(job, null) as string))
    .ToList();

   public partial class tblJob
    {
        public string ClientCode { get; set; }
        public string ClientName { get; set; }
        public string ClientURL { get; set; }
        public string JobCode { get; set; }
        public string JobName { get; set; }
        public string JobURL { get; set; }
        public string LongJobDescription { get; set; }
        public string iPowerLink { get; set; }
        public string Industry { get; set; }
        public string ChargeType { get; set; }
        public string Product { get; set; }
        public string ProductGroup { get; set; }
        public Nullable<decimal> GrossEstimatedFee { get; set; }
        public string LineOfService { get; set; }
        public string LineOfServiceCode { get; set; }
        public string LineOfServiceRole { get; set; }
        public string LineOfServiceRolePD { get; set; }
        public string LineOfServiceRoleMAPO { get; set; }
        public string LineOfServiceRoleMA { get; set; }
        public string LineOfServiceRoleSTAFF { get; set; }
        public string BusinessUnit { get; set; }
        public string BusinessUnitCode { get; set; }
        public string BusinessUnitRole { get; set; }
        public string BusinessUnitRolePD { get; set; }
        public string BusinessUnitRoleMAPO { get; set; }
        public string BusinessUnitRoleMA { get; set; }
        public string BusinessUnitRoleSTAFF { get; set; }
        public string OperatingUnit { get; set; }
        public string OperatingUnitCode { get; set; }
        public string OperatingUnitRole { get; set; }
        public string OperatingUnitRolePD { get; set; }
        public string OperatingUnitRoleMAPO { get; set; }
        public string OperatingUnitRoleMA { get; set; }
        public string OperatingUnitRoleSTAFF { get; set; }
        public int Terminated { get; set; }
        public Nullable<System.DateTime> TerminatedDate { get; set; }
        public string JobPartner { get; set; }
        public string JobManager { get; set; }
        public string JobDirector { get; set; }
        public string BillPartner { get; set; }
        public string BillManager { get; set; }
        public string JobTeam { get; set; }
        public string JobEntity { get; set; }
        public string ProductCode { get; set; }
        public string BillContact { get; set; }
        public Nullable<int> Confidential { get; set; }
    }

在此处输入图像描述

4

1 回答 1

1

尝试添加BindingFlags.Instance到绑定标志过滤器:BindingFlags.Public | BindingFlags.Instance.

更多关于绑定标志的信息可以在这里找到。

于 2013-07-04T14:29:20.063 回答