试图填写List <keypair <string,string>&gt;反射总是空的</keypair <string,string>

时间:2013-06-27 12:08:15

标签: c#

使用以下代码,我试图创建一个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; }
    }

enter image description here

1 个答案:

答案 0 :(得分:1)

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

有关绑定标志的更多信息,请here