how do I iterate through internal properties in c#

前端 未结 6 1127
鱼传尺愫
鱼传尺愫 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:23

    Use BindingFlags

    foreach (PropertyInfo property in typeof(TestClass)
            .GetProperties(
                BindingFlags.Public |
                BindingFlags.NonPublic |
                BindingFlags.Instance))
    {
        //do something
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-30 23:28

    You get the internal properties of a type by querying the NonPublic properties and then filtering the Get methods of these by "IsAssembly".

    "internal protected" properties have their getters marked as "IsFamilyOrAssembly", "protected" properties as "IsFamily", and "private" properties as marked with "IsPrivate":

        public class TestClass
        {
            public string Property1 { get; set; }
            private string Property2 { get; set; }
    
            public string Property9 { get; set; }
            private string Property10 { get; set; }
    
            protected internal string Property3 { get; set; }
            protected string Property4 { get; set; }
            internal string Property5 { get; set; }
    
            protected internal int Property6 { get; set; }
            protected int Property7 { get; set; }
            internal int Property8 { get; set; }
    
            internal static void ShowPropertyAccessScope(Type t)
            {
    
                foreach (var prop in t.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    Console.WriteLine("{0,-28} {1,15}", "Public property:", prop.Name);
                }
    
                var nonPublic = t.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic);
    
                foreach (var prop in nonPublic.Where(p => p.GetGetMethod(true)?.IsAssembly == true))
                {
                    Console.WriteLine("{0,-28} {1,15}", "Internal property:", prop.Name);
                }
    
                foreach (var prop in nonPublic.Where(p => p.GetGetMethod(true)?.IsFamilyOrAssembly == true))
                {
                    Console.WriteLine("{0,-28} {1,15}", "Internal protected property:", prop.Name);
                }
    
                foreach (var prop in nonPublic.Where(p => p.GetGetMethod(true)?.IsFamily == true))
                {
                    Console.WriteLine("{0,-28} {1,15}", "Protected property:", prop.Name);
                }
    
                foreach (var prop in nonPublic.Where(p => p.GetGetMethod(true)?.IsPrivate == true))
                {
                    Console.WriteLine("{0,-28} {1,15}", "Private property:", prop.Name);
                }
            }
            static void Main() 
            {
                ShowPropertyAccessScope(typeof(TestClass));
            }
        }
    
    0 讨论(0)
  • 2020-12-30 23:34

    You need to change the BindingFlags on your call to Type.GetProperties

    Try:

    var instanceProperties = typeof(TestClass).GetProperties(
        BindingFlags.Public |
        BindingFlags.NonPublic | 
        BindingFlags.Instance
    );
    foreach(var instanceProperty in instanceProperties) {
        // a little something something for the instanceProperty
    }
    
    0 讨论(0)
  • 2020-12-30 23:47

    by specifying what bindingflags in GetProperties:

    foreach (PropertyInfo property in typeof(TestClass).GetProperties(
          BindingFlags.Instance|
          BindingFlags.Public|
          BindingFlags.NonPublic))
    
    0 讨论(0)
  • 2020-12-30 23:48

    According to MSDN, private and internal are not recognized in Reflection API.

    To identify an internal method using Reflection, use the IsAssembly property. To identify a protected internal method, use the IsFamilyOrAssembly.

    If You are writing some test units You might want to take a look at InternalsVisibleTo attribute. It allows you to specify which assembly can see internal properties.

    And finally, do You really need to have internal properties...

    0 讨论(0)
提交回复
热议问题