如何通过反射将我创建的类型区分为系统类型?

Vin*_*oni 0 c# reflection

我想知道我传递的类型是系统类型还是我创建的类型.我怎么知道这个?看:

// Obs: currentEntity can be any entity that i created
var currentProperties = currentEntity.GetType().GetProperties();

foreach (var property in currentProperties)
{
    if (/* Verify here if the property is a system type */)
    {
         // Do what i want...
    }
}
Run Code Online (Sandbox Code Playgroud)

验证这个的最佳方法是什么?

OBS:作为"系统类型"计算所有类型的核心标准库,在由Microsoft签署的程序集中,如:DateTime,String,Int32,Boolean(mscorlib.dll中的所有类型| System.dll)...

OBS2:我的实体不会继承那些"系统类型".

OBS3:我的实体可以是我创建的任何类型,因此我无法在比较中指定.

OBS4:我需要进行比较而不指定它是否等于String,Boolean ...

Jon*_*eet 7

什么算作"系统"类型?您可以检查是否:

  • 它在mscorlib中
  • 它是在微软签署的集会中
  • 它是您认为事先是"系统"的固定类型之一
  • 它是在你认为是"系统"之前的一组固定组件之一
  • (容易伪造)它的命名空间是System或开始于System.

一旦你通过"系统"定义了你的意思,那几乎就是用来检查它的代码.例如:

  • if (type.Assembly == typeof(string).Assembly)
  • var publisher = typeof(string).Assembly.Evidence.GetHostEvidence<Publisher>();- 然后检查是否publisher拥有适用于Microsoft的正确证书
  • if (SystemTypes.Contains(type)) - 一旦你想出了自己的系统类型列表
  • if (SystemAssemblies.Contains(type.Assembly)) - 一旦你提出了自己的系统组件清单(更实用)

编辑:按照意见,如果你满意的公正mscorlibSystem.dll:

private static readonly ReadOnlyCollection<Assembly> SystemAssemblies = 
    new List<Assembly> {
        typeof(string).Assembly, // mscorlib.dll
        typeof(Process).Assembly, // System.dll
    }.AsReadOnly();

...

if (SystemAssemblies.Contains(type.Assembly))
{
    ...
}
Run Code Online (Sandbox Code Playgroud)