Reflection WTF

time to read 3 min | 419 words

This is a major WTF, in my opinon, consider the following code:

using System;
public class Program
 static void Main()
  Console.WriteLine( "Is Public: {0}"typeof(Inner).IsPublic );
  Console.WriteLine( "Is Not Public: {0}"typeof(Inner).IsNotPublic );
 public class Inner {}

Without compiling this, can you hazzard a guess about what is the result of this program?

Don't bother, here it is:

Is Public: False
Is Not Public: False

In a stunning misuse of their own guidlines and the English langauge, those are actually not mirror images of each other. Furthermore, check out the documentation for those properties:Type.IsPublic and Type.IsNotPublic. Ignore the documentation at the top of the page, it flat out misleading, check the Property Value section, it contains such gems as (IsNotPublic):

true if the Type is not declared public and is not a nested type; otherwise, false.

So, I can change the visibility of Inner to private, and the output remains the same. IsXXX is a well defined notation, this just completely breaks it. The issue here is the use of TypeAttributes to get the value, and TypeAttributes make a distiction between Public and NestedPublic, but why the hell do they take this distinction to the IsXXX properties. Either the value is public or it isn't, don't go put nesting issues there when you call the property IsPublic.