Search code examples
c#reflectionpropertiesstaticbindingflags

How to exclude static property when using GetProperties method


I was wonder if I can exclude static property when I'm using GetProperties() to extract all the property for a specific class. I'm aware of using BindingFlags for this to filter properties what I need but what I really want is that I want to exclude static properties. I try to use something like this:

typeof(<class>).GetProperties(!BindingFlags.Static);

but I don't think it works, because VS throw me some syntax error. Here is what inside my class with properties.

public class HospitalUploadDtl : Base.Tables
        {
            public HospitalUploadDtl() { }
            public HospitalUploadDtl(SqlDataReader reader)
            {
                ReadReader(reader);
            }
            #region Properties
            public long BatchDtlId { get; set; }
            public long BatchNumber { get; set; }
            public string HospitalCode { get; set; }
            public string HospitalName { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string Country { get; set; }
            public string ContractPerson { get; set; }
            public string ContactNo { get; set; }
            public string Email { get; set; }
            public bool isAccredited { get; set; }
            public bool isClinic { get; set; }
            public string FaxNo { get; set; }
            public string TypeofFacility { get; set; }
            public string Category { get; set; }
            public string Specialty { get; set; }
            public string ProviderName { get; set; }
            public bool CashlessInPatient { get; set; }
            public bool CashlessOutPatient { get; set; }
            #endregion

            public static dcHospitalUploadDtl dataCtrl;
            public static dcHospitalUploadDtl DataCtrl
            {
                get
                {
                    if (dataCtrl == null)
                        dataCtrl = new dcHospitalUploadDtl();
                    return dataCtrl;
                }
            }
        }

for this scenario I want to exclude "DataCtrl" property when I call for GetProperties(). Thanks for the response. :)


Solution

  • I think you're looking for BindingFlags.Instance | BindingFlags.Public (if you only include Instance, no properties will be found since neither Public nor NonPublic are specified).