简体   繁体   中英

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. :)

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

The call

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

does not do what you expect: the value passed in to GetProperties is a bit mask , not an expression. Only bitwise ORs are allowed inside the expression. In other words, you cannot say things that you don't want: you must say which things you do want. So instead of passing !BindingFlags.Static you should pass BindingFlags.Instance .

Alternatively, you can get all properties, and then apply LINQ with its rich filtering semantic to remove the items you don't need:

typeof(<class>).GetProperties().Where(p => !p.GetGetMethod().IsStatic).ToArray();

您应该使用BindingFlags.Instance标志。

typeof(HospitalUploadDtl).GetProperties(BindingFlags.Instance | BindingFlags.Public);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM