I have a little console application that I’m tinkering with just to learn something new.
In the code below, in Console.WirteLine(), if I test t.IsAbstract, or t.IsSealed, my output is AbstractClass true, or SealedClass true respectively. All others return false as I expect.
However, if I test t.IsPublic, everything, including both PublicClass and PublicInterface return false. Why is that?
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
class Test
{
static void Main(string[] args)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Type[] assemblyTypes = assembly.GetTypes();
foreach (Type t in assemblyTypes)
Console.WriteLine(t.Name + " " + t.IsPublic);
Console.ReadKey();
}
private class PrivateClass { }
public class PublicClass { }
protected class ProtectedClass { }
sealed class SealedClass { }
abstract class AbstractClass { }
interface myInterface { }
public interface PublicInterface { }
}
}
Because they are nested inside of
Test.From the documentation: true if the Type is declared public and is not a nested type; otherwise, false.
As @Jeb’s answer and the docs suggest,
typeof(PublicClass)should have a value of true for the IsNestedPublic property