how do I iterate through internal properties in c#
Asked Answered
D

6

36
public class TestClass
{
        public string property1 { get; set; }
        public string property2 { get; set; }

        internal string property3 { get; set; }
        internal string property4 { get; set; }
        internal string property5 { get; set; }
}

I can iterate through the properties with the following loop, but it only shows public properties. I need all the properties.

foreach (PropertyInfo property in typeof(TestClass).GetProperties())
{
    //do something
}
Danieldaniela answered 27/9, 2011 at 20:56 Comment(1)
Also see #16024506 for a variant for this.Archon
Z
76

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.

Zygophyllaceous answered 27/9, 2011 at 21:0 Comment(4)
Giving NonPublic and Instance will hide prop 1 and prop 2, you need both, NonPublic | Public for all propertiesMadeleinemadelena
anyway to select only protected or internal?Matelote
@NevilleNazerane: Not with binding flags, that I'm aware of. Just filter afterwards.Zygophyllaceous
The piece it took me a little while to figure out is that once you're specifying any BindingFlags, you need to specify all BindingFlags necessary to identify the props you're interested in; specifically, without BindingFlags.Instance you won't match on any instance properties. (other languages' introspection led me to expect "Instance" to be assumed)Apollonian
M
13

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
}
Maimaia answered 27/9, 2011 at 21:0 Comment(1)
yeah, I had to add BindingFlags.Public as wellDanieldaniela
I
7

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

Interbedded answered 27/9, 2011 at 21:7 Comment(1)
yeah I do, it's wcf service class, I want the client to fill in some properties before sending it to the server, and rest of the properties to be filled in on the service.Danieldaniela
C
3

Use BindingFlags

foreach (PropertyInfo property in typeof(TestClass)
        .GetProperties(
            BindingFlags.Public |
            BindingFlags.NonPublic |
            BindingFlags.Instance))
{
    //do something
}
Cuevas answered 27/9, 2011 at 21:1 Comment(0)
C
3

by specifying what bindingflags in GetProperties:

foreach (PropertyInfo property in typeof(TestClass).GetProperties(
      BindingFlags.Instance|
      BindingFlags.Public|
      BindingFlags.NonPublic))
Cumine answered 27/9, 2011 at 21:5 Comment(0)
R
2

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));
        }
    }
Referendum answered 9/7, 2019 at 1:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.