Luis Valencia
Luis Valencia

Reputation: 34038

Trying to fill a List<Keypair<string,string>> with reflection is always empty

With the following code, I am trying to create a List of KeyValuePair. However its always empty. Why?

During debug time you can see that the properties include the word Role on their name. I also checked the type tblJob, and all those properties are string. See screenshot.

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

Upvotes: 0

Views: 118

Answers (1)

vc 74
vc 74

Reputation: 38179

Try to add BindingFlags.Instance to the binding flags filter: BindingFlags.Public | BindingFlags.Instance.

More on binding flags can be found here.

Upvotes: 1

Related Questions