我有一個小控制檯應用程序,我正在修補,只是爲了學習新的東西。爲什麼System.Type.IsPublic返回false?
在下面的代碼,在Console.WirteLine()
,如果我測試t.IsAbstract
,或t.IsSealed
,我的輸出分別是AbstractClass true
,或SealedClass true
。按照我的預期,所有其他人都會返回false
。
但是,如果我測試t.IsPublic
,則包括PublicClass
和PublicInterface
在內的所有內容均返回false
。 這是爲什麼?
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 { }
}
}
我的+1分析是錯誤的,克里斯答案是正確的 – JaredPar 2012-02-24 18:52:26