如何使用反射来确定类是否为内部类? [英] How to use reflection to determine if a class is internal?

查看:57
本文介绍了如何使用反射来确定类是否为内部类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,如何使用反射检查类定义是否定义为内部?"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 and internal have no meaning in IL and are not used in the Reflection APIs. The corresponding terms in IL are Family and Assembly. To identify an internal method using Reflection, use the IsAssembly property. To identify a protected internal method, use the IsFamilyOrAssembly.

Reflection不能对 Type 进行检查,以检查它是否是内部受保护的受保护的内部.

Reflection does not expose a way on Type check if it is internal, protected or protected internal.

这篇关于如何使用反射来确定类是否为内部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆