4

I see that the "Quick Watch" window has access to all properties, irrespective of access restrictions (internal, protected, private) of a class in a library, even when the library is referenced in a totally different app,lib and namespace. Whereas I am not finding a way to access these using "reflection". I am especially trying to "read" (note - just read) the internal property of an assembly. If this is not possible by design of how "internal" works (not accessible outside the same namespace), how come it is "read" by the "Quick watch" window in VS.NET?

Here is the example code I used:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestLib
{
    public class TestInteralProp
    {
        internal string PropertyInternal
        {
            get { return "Internal Property!";  }
        }

        protected string PropertyProtected
        {
            get { return "Protected Property!"; }
        }

        string PropertyPrivate
        {
            get { return "Private Property!"; }
        }

        public string PropertyPublic
        {
            get { return "Public Property!";  }
        }

        protected internal string PropertyProtectedInternal
        {
            get { return "Protected Internal Property!"; }
        }
    }
}

When i create an object for TestInernalProp class, I can see all 4 properties in quickwatch -

props

And when I use reflection to access any of these except the public property (PropertyPublic), i get a null reference exception.

//this works
propTestObj.GetType().InvokeMember("PropertyPublic", System.Reflection.BindingFlags.GetProperty, null, propTestObj, null);
//this fails (obviously)
propTestObj.GetType().InvokeMember("PropertyInternal", System.Reflection.BindingFlags.GetProperty, null, propTestObj, null);

//this works
propTestObj.GetType().GetProperty("PropertyPublic").GetValue(propTestObj, null);
//this fails again
propTestObj.GetType().GetProperty("PropertyInternal").GetValue(propTestObj, null)

I am not clear about how "Quick Watch" can gain access to these.

Lalman
  • 946
  • 2
  • 11
  • 27
  • 1
    possible duplicate of [Properties not visible at design time but only at runtime](http://stackoverflow.com/questions/9666081/properties-not-visible-at-design-time-but-only-at-runtime) – H H Mar 12 '12 at 13:18
  • are you trying to reflect in a different assembly? I know there are weird rules involving internal. More info: [MSDN Internal](http://msdn.microsoft.com/en-us/library/7c5ka91b(v=vs.80).aspx) – Tin Can Mar 12 '12 at 13:20
  • 1
    *Quick Watch* is part of the VS, which is C++ and COM application. It may have direct access to the internal structure of the managed process, i.e. to the [MethodDesc](http://msdn.microsoft.com/en-us/magazine/cc163791.aspx#S11) table. – oleksii Mar 12 '12 at 13:20
  • 1
    @Tin, I was actually reflecting it in a different assembly. It works with bindingflags reset as told by Olivier below. It is also quite understood from BrokenGlas's comments below that how tools like reflector are able to reflect internals of almost any assembly. – Lalman Mar 12 '12 at 13:38

1 Answers1

12

Use these flags

BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance

The GetProperty flag is not required for this operation. You might want to add Static as well.

Note: You can combine the flags with |, because their integer values are powers of two. See this SO answer.


NOTE (In response to Lalman's and shanks's concern about security issues of Reflection)

There is always a way to write bad or dangerous code. It is up to you to do it or not. Reflection is not the regular way of doing things, rather is it meant for the programming of special tools, like o/r-mappers or analysis tools. Reflection is very powerful; however, you should use it wisely.

Community
  • 1
  • 1
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • That was cool, did the trick. Thanks Olivier! I could see all properties including private ones. Just curious - this also means that there should **also be a way to restrict this** ? otherwise hiding internal code can never be possible. – Lalman Mar 12 '12 at 13:27
  • 1
    if you have the rights for reflection there is no way to restrict this - e.g. in Silverlight you cannot do reflection, so that is a "global" restriction – BrokenGlass Mar 12 '12 at 13:31