how do I iterate through internal properties in c#

前端 未结 6 1180
鱼传尺愫
鱼传尺愫 2020-12-30 23:07
public class TestClass
{
        public string property1 { get; set; }
        public string property2 { get; set; }

        internal string property3 { get; set; }         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-30 23:24

    You need to specify that you don't just need the public properties, using the overload accepting BindingFlags:

    foreach (PropertyInfo property in typeof(TestClass)
                 .GetProperties(BindingFlags.Instance | 
                                BindingFlags.NonPublic |
                                BindingFlags.Public))
    {
        //do something
    }
    

    Add BindingFlags.Static if you want to include static properties.

    The parameterless overload only returns public properties.

提交回复
热议问题