Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I look up the internal properties of a C# class? protected? protected internal?

If I have a C# class MyClass as below:

using System.Diagnostics;

namespace ConsoleApplication1
{
    class MyClass
    {
        public int pPublic {get;set;}
        private int pPrivate {get;set;}
        internal int pInternal {get;set;}
    }
    class Program
    {
        static void Main(string[] args)
        {
            Debug.Assert(typeof(MyClass).GetProperties(
                System.Reflection.BindingFlags.Public |
                System.Reflection.BindingFlags.Instance).Length == 1);
            Debug.Assert(typeof(MyClass).GetProperties(
                System.Reflection.BindingFlags.NonPublic |
                System.Reflection.BindingFlags.Instance).Length == 2);
            // internal?
            // protected?
            // protected internal?
        }
    }
}

The code above compiles are runs without any assertion failures. NonPublic returns the Internal and Private properties. There does not appear to be flags for the other accessibility types on BindingFlags.

How do I get a list/array of only the properties that are internal? On a related note, but not necessary for my application, what about protected, or protected internal?

like image 416
ryantm Avatar asked Apr 15 '13 20:04

ryantm


People also ask

What is an internal property C#?

It means that the property can only be set by code that resides within the same assembly as the class delcaring the property.

What is the meaning of Internal property?

By an internal property of a thing, I mean a property of a thing such that the thing could not but have it. The whole question has been discussed more often with regard to relational properties.

What does internal set mean?

In mathematical logic, in particular in model theory and nonstandard analysis, an internal set is a set that is a member of a model.


2 Answers

When you get the property infos with BindingFlags.NonPublic, you find the getter or setter by using GetGetMethod(true) and GetSetMethod(true), respectively. You can then check the following properties (of the method info) to get the exact access level:

  • propertyInfo.GetGetMethod(true).IsPrivate means private
  • propertyInfo.GetGetMethod(true).IsFamily means protected
  • propertyInfo.GetGetMethod(true).IsAssembly means internal
  • propertyInfo.GetGetMethod(true).IsFamilyOrAssembly means protected internal
  • propertyInfo.GetGetMethod(true).IsFamilyAndAssembly means private protected

and similarly for GetSetMethod(true) of course.

Remember that it is legal to have one of the accessors (getter or setter) more restricted than the other one. If there is just one accessor, its accessibility is the accessibility of the entire property. If both accessors are there, the most accessible one gives you the accessibility of the whole property.

Use propertyInfo.CanRead to see if it's OK to call propertyInfo.GetGetMethod, and use propertyInfo.CanWrite to see if it's OK to call propertyInfo.GetSetMethod. The GetGetMethod and GetSetMethod methods return null if the accessor does not exist (or if it is non-public and you asked for a public one).

like image 93
Jeppe Stig Nielsen Avatar answered Sep 22 '22 12:09

Jeppe Stig Nielsen


See this article on MSDN.

Relevant quote:

The C# keywords protected and internal have no meaning in IL and are not used in the reflection APIs. The corresponding terms in IL are Family and Assembly. To identify an internal method using reflection, use the IsAssembly property. To identify a protected internal method, use the IsFamilyOrAssembly.

like image 40
500 - Internal Server Error Avatar answered Sep 20 '22 12:09

500 - Internal Server Error