如何使用反射来确定类是否为内部类? [英] How to use reflection to determine if a class is internal?
问题描述
正如标题所述,如何使用反射检查类定义是否定义为内部?"typeof(...)"返回下面显示的某些属性,但不返回是否将类定义为内部属性.在Google上看过,但我能找到的很多文章都是关于使用反射运行内部方法或受保护方法的.在这种情况下,这不是我感兴趣的方法,而是类的定义.
As the title says, how do you use reflection to check if a class definition is defined as internal? "typeof(...)" returns certain properties shown below but not whether a class is defined as internal. Looked on Google but all I could find were lots of articles about running internal or protected methods using reflection. It's not the methods I'm interested in this case, but the class definition.
var type = typeof(Customer);
Assert.IsTrue(type.IsClass);
Assert.That(type.IsAbstract, Is.EqualTo(isAbstract));
Assert.That(type.IsPublic, Is.EqualTo(isPublic));
Assert.That(type.IsPublic, Is.EqualTo(isPublic));
Assert.That(type.IsSealed, Is.EqualTo(isSealed));
Assert.That(type.IsSerializable, Is.EqualTo(isSerializable));
推荐答案
这是一个经典问题.来自 MSDN :
This is a classic issue. From MSDN:
C#关键字
protected
和internal
在IL中没有意义,因此未在反射API中使用.IL中的相应术语是Family
和Assembly
.要使用反射标识内部
方法,请使用IsAssembly
属性.要确定受受保护的内部
方法,请使用IsFamilyOrAssembly
.
The C# keywords
protected
andinternal
have no meaning in IL and are not used in the Reflection APIs. The corresponding terms in IL areFamily
andAssembly
. To identify aninternal
method using Reflection, use theIsAssembly
property. To identify aprotected internal
method, use theIsFamilyOrAssembly
.
Reflection不能对 Type
进行检查,以检查它是否是内部
,受保护的
或受保护的内部
.
Reflection does not expose a way on Type
check if it is internal
, protected
or protected internal
.
这篇关于如何使用反射来确定类是否为内部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!